How to conditionally compile for tvOS in Swift

This is also covered by Apple under the heading Targeting Apple TV in Your Apps

Listing 1-1 Conditionalizing code for tvOS in Objective-C

 #if TARGET_OS_TV
     NSLog(@"Code compiled only when building for tvOS.");
 #endif

Listing 1-2 Conditionalizing code for tvOS in Swift

#if os(tvOS)
NSLog(@"Code compiled only when building for tvOS.");
#endif

if #available(tvOS 9.1,*) {
    print("Code that executes only on tvOS 9.1 or later.")
}

#if os(OSX)
// compiles for OS X
#elseif os(iOS)
// compiles for iOS
#elseif os(tvOS)
// compiles for TV OS
#elseif os(watchOS)
// compiles for Apple watch
#endif

Tags:

Ios

Swift

Tvos