I am developing an application for CarPlay, and I would like the user to be able to make a call directly from CarPlay when they tap the "make a call" button. However, right now, it opens the confirmation on the connected smartphone. How can I make the call happen directly from CarPlay?
private func makePhoneCall(phoneNumber: String) {
guard let url = URL(string: "tel://\(phoneNumber)"), UIApplication.shared.canOpenURL(url) else {
print("Error to make call.")
return
}
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
func templateApplicationScene(_ templateApplicationScene: CPTemplateApplicationScene, didConnect interfaceController: CPInterfaceController) {
self.interfaceController = interfaceController
showCallConfirmation(phoneNumber: "199999999999")
}
private func showCallConfirmation(phoneNumber: String) {
guard let interfaceController = self.interfaceController else {
print("Interface Controller is not available")
return
}
let confirmAction = CPAlertAction(title: "Call Me", style: .default) { [weak self] _ in
self?.makePhoneCall(phoneNumber: phoneNumber)
}
let cancelAction = CPAlertAction(title: "Cancel", style: .cancel) { _ in
}
let alertTemplate = CPAlertTemplate(titleVariants: ["Do you want make phone call? \(phoneNumber)?"],
actions: [confirmAction, cancelAction])
interfaceController.presentTemplate(alertTemplate, animated: true)
}