TARGET_OS_IOS, TARGET_OS_TV and simulators

4.2k views Asked by At

I've "ported" an iOS App to Apple TV and since I really wanted to share as resources as possible I had to target some lines of code for iOS and some others to TVOS. I've tried with something like:

#if TARGET_OS_TV

and

#if TARGET_OS_IOS

but when I launch the apps on iOS or TV simulator this code doesn't work. I thought that iPhone simulator just executes the code under TARGET_OS_IOS... but I was wrong. Which is the best way to target iOS and TV os preserving simulators correct execution?

An example of the code that I might need is:

#if TARGET_OS_IOS 
    DoSomethingWithiOS() // This should work also on iOS sim
#elseif TARGET_OS_TV
    DoSomethingWithOSTV() // This should work also on TV sim
#endif
1

There are 1 answers

0
ManWithBear On BEST ANSWER

For someone looking for platform macro as me.

In ObjC use TARGET_OS_IOS for iOS and TARGET_OS_TV for tvOS as next:

#if TARGET_OS_IOS
    /// [something doIOS];
#elif TARGET_OS_TV
    /// [something doTVOS];
#else
    /// Perhaps watchOS or macOS?
#endif