0

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)
    }

1 Answer 1

0

You have the right idea, opening a tel url, but you are getting the prompt on the phone because you are using UIApplication.shared.open.

You need to call the equivalent open function on the UIScene that was passed to your CPTemplateApplicationSceneDelegate when the scene connected to your CarPlay app.

Not the answer you're looking for? Browse other questions tagged or ask your own question.