SwiftUI For AutoMaker CarPlay lifecycle

I produce a swiftui app and I integrate AutoMaker CarPlay(com.apple.developer.carplay-protocols) into my app.In CarPlay simulator,We can see auto maker app,but it is blank.

Try explicitly accessing UISceneDelegate and providing the window you want to show.

For example, declare a UIApplicationDelegateAdaptor:

    @UIApplicationDelegateAdaptor(AppDelegate.self) private var appDelegate
    var body: some Scene {
        WindowGroup {
            ContentView(shared: MemoryLogger.shared)
        }
    }
}

Then in your AppDelegate class, you'd implement the following protocol method to provide a scene configuration:

        _ application: UIApplication,
        configurationForConnecting connectingSceneSession: UISceneSession,
        options: UIScene.ConnectionOptions
    ) -> UISceneConfiguration {
        
        let configuration = UISceneConfiguration(
            name: "SceneConfiguration",
            sessionRole: connectingSceneSession.role)
        return configuration
    }

Finally in your UISceneDelegate, provide the View or UIViewController you want to show when CarPlay connects:

        if let scene = scene as? UIWindowScene, let delegate = CarScreenManager.manager.delegate {
            let screen = scene.screen
            if screen.traitCollection.userInterfaceIdiom == .carPlay && carWindow == nil {
                let windowRect = screen.bounds
                let outputWindow = UIWindow(frame: windowRect)
                outputWindow.autoresizesSubviews = true
                outputWindow.autoresizingMask = [.flexibleHeight, .flexibleWidth]
                outputWindow.windowScene = scene
                outputWindow.makeKeyAndVisible()
                outputWindow.isHidden = false
                
                CarScreenManager.manager.carViewController = delegate.rootVC(carScene: scene)
                outputWindow.rootViewController = CarScreenManager.manager.carViewController
                carWindow = outputWindow
            }
        }
    }

Once you detect the connected scene you should be able to provide a view.

SwiftUI For AutoMaker CarPlay lifecycle
 
 
Q