ios - 将 CarPlay 添加到 SwiftUI 生命周期应用程序

标签 ios swift swiftui carplay

将 CarPlay 集成到使用 SwiftUI 生命周期的应用程序中的推荐方法是什么?

@main
struct MyApp: App {
  var body: some Scene {
    WindowGroup {
      ContentView()
    }
  }
}

如何在此处使用 CPTemplateApplicationSceneDelegate

最佳答案

如果您没有自定义应用程序委托(delegate)和/或场景委托(delegate)(您最终可能需要它来处理推送通知等内容),那么通过 info.plist 让您的应用程序知道就足够了。您需要 CarPlay 场景的场景委托(delegate),并将以下内容添加到您的 info.plist 中:

<dict>
    <key>UIApplicationSupportsMultipleScenes</key>
    <true/>
    <key>UISceneConfigurations</key>
    <dict>
        <key>CPTemplateApplicationSceneSessionRoleApplication</key>
        <array>
            <dict>
                <key>UISceneClassName</key>
                <string>CPTemplateApplicationScene</string>
                <key>UISceneConfigurationName</key>
                <string>TemplateSceneConfiguration</string>
                <key>UISceneDelegateClassName</key>
                <string>AppFeature.CarPlaySceneDelegate</string>
            </dict>
        </array>
    </dict>
</dict>

UISceneConfigurationName 的值在场景委托(delegate)的 scene(_:willConnectTo:options:) session.configuration.name 中传递给您。 UISceneDelegateClassName 的值必须与 CarPlay 场景委托(delegate)的类型名称匹配。请注意,如果将 CarPlay 代码封装在包/框架中,则需要在委托(delegate)名称前加上模块名称前缀(在本例中为 AppFeature)。如果委托(delegate)位于您的应用目标中,只需使用 CarPlaySceneDelegate

场景委托(delegate)的摘录可能如下所示:

class CarPlaySceneDelegate: UIResponder, CPTemplateApplicationSceneDelegate {
  let templateManager = TemplateManager() // see Apple's sample code

  func templateApplicationScene(_: CPTemplateApplicationScene, didConnect interfaceController: CPInterfaceController) {
    templateManager.connect(interfaceController)
  }

  func templateApplicationScene(_: CPTemplateApplicationScene, didDisconnectInterfaceController _: CPInterfaceController) {
    templateManager.disconnect()
  }
}

关于ios - 将 CarPlay 添加到 SwiftUI 生命周期应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72896480/

相关文章:

javascript - 在 React Native 中——是否可以在通过 Prop 接收到的动画值上添加一个监听器?

ios - Swift:TextView 字体在模拟器和 iPhone 中的行为不同

iOS:使用 for-in 循环管理 Dictionray 数据

ios - 如何根据键盘大小正确实现 View 的缩小和扩展?

ios - 单个 UIViewController 中的多个 Collection View 未调用 cellForItemAtIndexPath indexPath

xcode - SwiftUI 错误 - 列表更改锁定 UI -(旧标题 : SwiftUI CoreData fetch is very slow)

ios - B2B 应用的 AppStore 问题

json - 使用 Swift 4 的 Decodable 解码 Void

ios - SwiftUI:有条件地隐藏 View 而不重新创建它

ios - 将 View 作为标题嵌入 SwiftUI 导航栏中