0

When using CarPlay's setRootTemplate method, I am unable to get either the completion handler or async/await to work properly. I need to perform some actions after the root template is loaded, but neither of these options allow for this.

This method is documented here, saying:

CarPlay calls completion after it presents the template. The Boolean parameter is true when the presentation succeeds; otherwise, it’s false and CarPlay provides an error that describes the failure. CarPlay throws an exception if the presentation fails and you don’t provide a closure.

Example 1: Completion Handler

interfaceController.setRootTemplate(tabBarTemplate, animated: false) { didSucceed, error in
    print("This completion handler never runs...")
}

Note: The above does work if you pass nil to the completion argument.

Example 2: Async/await

    do {
        print("This appears in console...")

        try await interfaceController.setRootTemplate(tabBarTemplate, animated: true)

        print("This doesn't...")
    } catch {
        print("error \(error.localizedDescription)")
    }

The synchronous version of this method does work, but it throws a deprecation warning in iOS 15.

Is this a bug, or is there something obvious that I'm missing?

Jeff C.
  • 2,031
  • 2
  • 17
  • 28
  • Are you testing on a real device? – fruitcoder Feb 01 '22 at 07:46
  • Excellent question! I am not. I only have the entitlement allowing deployment using the previous CarPlay API and am waiting on Apple to approve the updated entitlement. Until that happens I am stuck working with the Simulator. Does this work differently with one versus the other? – Jeff C. Feb 02 '22 at 08:27
  • The simulator has definitely been a big point of frustration with big differences in behavior compared to a real device (and sometimes even simulators from other minor Xcode releases :) that being said on my current Xcode Version 13.2.1 (13C100) the completion handler of `setRootTemplate` is called with a successful flag and a `nil` error. I set the template in the `CPTemplateApplicationSceneDelegate`'s `templateApplicationScene(_:didConnect:)` – fruitcoder Feb 02 '22 at 13:31
  • +1 Avoid doing any dev work on the sims. Other than basic setup you run into a lot of frustrating issues resolved on physical devices. – Harry J Feb 28 '22 at 05:28
  • I'm having the same issue as you had testing on a real device with a Carplay simulator. Were you have to fix the issue? – mik.corcuera Jun 13 '22 at 20:08
  • I found that you can only set a particular template instance as the root template *once* - I was trying to switch between two different templates deepening on state, but the second time I tried to set a particular template instance as the root it did not appear and there was no completion handler invocation. Using a fresh instance of the template each time worked perfectly. – Paulw11 Nov 07 '22 at 09:14

1 Answers1

0

As per the apple developer documentation setRootTemplate has 3 parameters.

I found a wrong syntax in your code.

use completion argument as below

self.interfaceController?.setRootTemplate(listTemplate, animated: true, completion: { success, error in
  print("onCompletion")
  print(success)
  print(error)
})

hope it helps...