我正在为 CarPlay 开发一个应用程序,我希望用户在点击“拨打电话”按钮时能够直接从 CarPlay 拨打电话。然而,现在,它会在连接的智能手机上打开确认信息。如何直接从 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)
}
您的想法是正确的,打开了
tel
网址,但您在手机上收到提示,因为您正在使用 UIApplication.shared.open
。
您需要在 open
上调用等效的
UIScene
函数,该函数在 CarPlay 应用程序启动时传递给您的 CPTemplateApplicationSceneDelegate
。