我有一个适用于 iOS 和 Mac Catalyst 的多平台应用程序。我正在尝试包含 Apple TV,但当我尝试构建时它显示
No such module 'CarPlay'
。
由于 Apple TV 不是 Target,我如何从 Apple TV 中排除 CarPlay 内容?
import CarPlay
class CarPlaySceneDelegate: UIResponder, CPTemplateApplicationSceneDelegate {
var interfaceController: CPInterfaceController?
func templateApplicationScene(_ templateApplicationScene: CPTemplateApplicationScene, didConnect interfaceController: CPInterfaceController) {
self.interfaceController = interfaceController
let nowPlaying = CPNowPlayingTemplate.shared
self.interfaceController?.setRootTemplate(nowPlaying, animated: true, completion: {_, _ in })
}
private func templateApplicationScene(_ templateApplicationScene: CPTemplateApplicationScene, didDisconnect interfaceController: CPInterfaceController) {
self.interfaceController = nil
}
}
tvOS 是一个单独的操作系统,Swift 支持基于目标操作系统的条件编译,因此您可以声明使用
CarPlay
的代码仅在不针对 tvOS
时可用。
#if !os(tvOS)
import CarPlay
class CarPlaySceneDelegate: UIResponder, CPTemplateApplicationSceneDelegate {
...
}
#endif
您也可以使用
#if canImport(CarPlay)
代替。