iosswiftcarplay

CarPlay app crashes when set root template - Unsupported object CPMapTemplate


I try to set root template as CPTabBarTemplate with CPMapTemplate and CPListTemplate. I get this message.

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unsupported object <CPMapTemplate: 0x600003712400> <identifier: FA0130C3-E609-4D78-94C4-1F88BBA9C181, userInfo: (null), tabTitle: Map, tabImage: (null), showsTabBadge: 0> passed to validateTemplates:. Allowed classes: {( CPInformationTemplate, CPPointOfInterestTemplate, CPGridTemplate, CPListTemplate )}' *** First throw call stack: ( 0 CoreFoundation 0x00007ff80049b761 __exceptionPreprocess + 242 1 libobjc.A.dylib
0x00007ff800063904 objc_exception_throw + 48 2 CoreFoundation
0x00007ff80049b63f -[NSException initWithCoder:] + 0 3 CarPlay
0x00007ff837772255 -[CPTabBarTemplate validateTemplates:] + 584 4
CarPlay 0x00007ff837771da8 -[CPTabBarTemplate initWithTemplates:] + 88

The code is pretty straightforward:

        let mapTemplate = CPMapTemplate()
        mapTemplate.tabTitle = "Map"
        
        let listTemplate = CPListTemplate(title: "List", sections: [CPListSection(items: [CPListItem(text: "Title", detailText: "desctiption")])])
        listTemplate.tabTitle = "List"
        
        let tabBarTemplate = CPTabBarTemplate(templates: [mapTemplate, listTemplate])
        self.interfaceController?.setRootTemplate(tabBarTemplate, animated: true, completion: nil)

But if I remove CPMapTemplate, I will see a tab bar template with a list template without any crashes. What is the reason for that crash?

I use simulator iOS 17, Xcode 15.2, Intel, Entitlements key com.apple.developer.carplay-maps

Here is some inspiration of expected design

enter image description here


Solution

  • The exception message tells you exactly what the problem is. You can't add a CPMapTemplate to a CPTabBarTemplate:

    Allowed classes: {( CPInformationTemplate, CPPointOfInterestTemplate, CPGridTemplate, CPListTemplate )}

    A CPMapTemplate can only be installed as the root template of your CarPlay app.

    You will need to rethink the design of your app. For example, you could show the list modally in response to a button tap.

    Based on the design you have shown, you probably want a EV charger "point of interest" CarPlay app. This will let you use a CPTabBarTemplate containing CPPointOfInterestTemplate.