Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS SwiftUI CarPlay "Hello World"

I have a full SwiftUI project/app (In the AppStore) that I would like to add some CarPlay functionality. I have the entitlement correctly setup and am able to see the icon on the CarPlay simulator.

I have tried various info.plist configurations with various class configurations and only achieve crashes at the moment when starting the app in the simulator.

I'm a bit confused about the way to go about displaying views. I would like to start with a simple "Hello world" but even that is challenging :)

This question seems to be the kind of thing that I require but doesn't go in to enough details on the AppDelegate configuration.

iOS 15.4 - SwiftUI + CarPlay - State not updating

This looks promising but again not enough detail for me:

https://dev.to/nitricware/adding-carplay-to-a-swiftui-life-cycle-app-h9h

This too , but I suspect it's an older way of displaying the views.

https://www.adapptor.com.au/blog/enhance-existing-apps-with-carplay

If I could get help with a simple "Hello World" I would be most grateful!

From info.plist

 <key>UIApplicationSceneManifest</key>
        <dict>
            <key>UIApplicationSupportsMultipleScenes</key>
            <true/>
            <key>UISceneConfigurations</key>
            <dict>
                <key>CPTemplateApplicationSceneSessionRoleApplication</key>
                <array>
                    <dict>
                        <key>UISceneDelegateClassName</key>
                        <string>$(PRODUCT_MODULE_NAME).CarPlaySceneDelegate</string>
                    </dict>
                </array>
            </dict>
        </dict>

CarPlaySceneDelegate.swift

import Foundation
import CarPlay

class CarPlaySceneDelegate: UIResponder, CPTemplateApplicationSceneDelegate {
    
  func templateApplicationScene(_ templateApplicationScene: CPTemplateApplicationScene,
                                  didConnect interfaceController: CPInterfaceController) {
    
    let screen = CPInformationTemplate(title: "Root", layout: .leading, items: [CPInformationItem(title: "Hello", detail: "CarPlay")], actions: [])
    
    interfaceController.setRootTemplate(screen, animated: true, completion: { _,_ in
        // Do nothing
    })
  }
}

Thanks

like image 372
jat Avatar asked Sep 06 '25 00:09

jat


2 Answers

I had been having the same issue as described above. I was trying to add CarPlay functionality to an existing SwiftUI app; but, launching the app in CarPlay resulted in the 'NSGenericException', reason: 'Application does not implement CarPlay template application lifecycle methods in its scene delegate.' error.

I downloaded Paulw11's CPHelloWorld project (Thanks, Paul!) and got it to function as expected. I then compared the CPHelloWorld's entitlements, info.plist, CarPlaySceneDelegate, etc. with mine, and everything seemed to be the same. Yet, his worked and mine didn't. I then started a new SwiftUI template project and brought the CarPlay functionality in from CPHelloWorld and confirmed it worked. Then, I brought in all the source code, package dependencies, and project settings from my project - checking at every step that the CarPlay app still launched correctly. Eventually I had my app pieced back together in this new project with a functioning CarPlay scene.

So why was my original attempt still failing? I started comparing build settings and found something involving scenes that was different. I the apps that worked with CarPlay the "Application Scene Manifest (Generation)" was set to NO. In my original app, (which wasn't working despite the entitlements, info.plist, and CarPlaySceneDelegate being the same), this was set to YES. No idea when it got turned on, but there it was.

I went back to my not-working-with-CarPlay app and turned off this build setting ("Application Scene Manifest (Generation)" == NO), and then I could launch my app in CarPlay and see the root template in the simulator.

Target -> Build Settings -> "Application Scene Manifest (Generation)" == NO

like image 168
Scott Muddlegeist Avatar answered Sep 07 '25 21:09

Scott Muddlegeist


Not sure if it will change anything but your dictionary in Indi.plist seems incomplete , try this one :

<key>UISceneConfigurations</key>
<dict>
    <key>CPTemplateApplicationSceneSessionRoleApplication</key>
    <array>
        <dict>
            <key>UISceneClassName</key>
            <string>CPTemplateApplicationScene</string>
            <key>UISceneDelegateClassName</key>
            <string>$(PRODUCT_MODULE_NAME).CarPlaySceneDelegate</string>
            <key>UISceneConfigurationName</key>
            <string>CarPlay</string>
        </dict>
    </array>
</dict>
like image 45
Ptit Xav Avatar answered Sep 07 '25 19:09

Ptit Xav