swift 从Apple TV目标中排除CarPlay内容

jgovgodb  于 2023-09-30  发布在  Swift
关注(0)|答案(1)|浏览(110)

我有一个多平台的应用程序为iOS和Mac催化剂。我试图包括苹果电视,但它说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
    }
}
egmofgnx

egmofgnx1#

tvOS是一个独立的操作系统,Swift支持基于目标操作系统的条件编译,因此您可以声明使用CarPlay的代码仅在不针对tvOS时可用。

#if !os(tvOS)
import CarPlay

class CarPlaySceneDelegate: UIResponder, CPTemplateApplicationSceneDelegate {
...
}

#endif

也可以使用#if canImport(CarPlay)

相关问题