iosswifttelevisioncarplay

excluding CarPlay stuff from Apple TV destination


I have a multi-platform app for iOS, and Mac Catalyst. I'm trying to include Apple TV, but it says No such module 'CarPlay' when I try to build.

Since Apple TV is not a Target, how can I exclude the CarPlay stuff from the Apple TV ?

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
    }
}

Solution

  • tvOS is a separate operating system and Swift support conditional compilation based on the targeted OS, so you can declare your code that uses CarPlay to only be available when not targeting tvOS.

    #if !os(tvOS)
    import CarPlay
    
    class CarPlaySceneDelegate: UIResponder, CPTemplateApplicationSceneDelegate {
    ...
    }
    
    #endif
    

    You could also use #if canImport(CarPlay) instead.