iOS 15下开发CarPlay应用UIWindowScene运行异常崩溃如何修复
崩溃根因
报错信息已经明确指出错误:你将CarPlay专用的CPTemplateApplicationScene类绑定到了iPhone主应用使用的UIWindowSceneSessionRoleApplication场景角色上,系统要求该角色的场景类必须继承自UIWindowScene,类型不匹配直接触发崩溃。
修复步骤
1. 修正Info.plist的Scene配置
你需要将iPhone主应用场景和CarPlay场景分开配置,替换原有的UIApplicationSceneManifest节点内容:
<key>UIApplicationSceneManifest</key> <dict> <key>UIApplicationSupportsMultipleScenes</key> <true/> <key>UISceneConfigurations</key> <dict> <!-- iPhone主应用场景配置(如果不需要iPhone端运行可删除该节点) --> <key>UIWindowSceneSessionRoleApplication</key> <array> <dict> <key>UISceneConfigurationName</key> <string>Default Configuration</string> <key>UISceneDelegateClassName</key> <string>$(PRODUCT_MODULE_NAME).SceneDelegate</string> <key>UISceneStoryboardFile</key> <string>Main</string> </dict> </array> <!-- CarPlay专用场景配置 --> <key>CPTemplateApplicationSceneSessionRoleApplication</key> <array> <dict> <key>UISceneClassName</key> <string>CPTemplateApplicationScene</string> <key>UISceneConfigurationName</key> <string>CarPlay</string> <key>UISceneDelegateClassName</key> <string>$(PRODUCT_MODULE_NAME).CarPlaySceneDelegate</string> </dict> </array> </dict> </dict>
注意:如果你的应用不需要在iPhone端独立运行,可以删除UIWindowSceneSessionRoleApplication对应的节点。
2. 修正AppDelegate的场景配置返回逻辑
原代码会给所有类型的场景都返回CarPlay配置,需要按角色区分返回:
@available(iOS 13.0, *) func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { // 判断是否为CarPlay场景 if connectingSceneSession.role == UISceneSession.Role(rawValue: "CPTemplateApplicationSceneSessionRoleApplication") { return UISceneConfiguration(name: "CarPlay", sessionRole: connectingSceneSession.role) } // 主应用场景返回默认配置 return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) }
3. 修复CarPlaySceneDelegate中的Tab配置错误
原代码所有tabSystemItem都赋值给了tabA,导致四个Tab配置全部异常,修正对应代码:
let tabA: CPListTemplate = listTemplate tabA.tabSystemItem = .favorites tabA.showsTabBadge = false let tabB: CPListTemplate = listTemplateA tabB.tabSystemItem = .mostRecent tabB.showsTabBadge = true let tabC: CPListTemplate = listTemplateB tabC.tabSystemItem = .history tabC.showsTabBadge = false let tabD: CPListTemplate = listTemplateC tabD.tabSystemItem = .search tabD.showsTabBadge = false
4. 可选校验项
确认你的项目entitlements文件中已添加对应CarPlay权限,比如音频类应用需要添加com.apple.developer.carplay-audio,导航类应用需要添加com.apple.developer.carplay-navigation,权限类型需要和你申请的CarPlay权限匹配。
内容的提问来源于stack exchange,提问作者Donat Treszler
相关产品推荐
相关产品推荐

