Preprocessor macro for Apple Watch?

jww picture jww · Jan 7, 2015 · Viewed 8.2k times · Source

I was looking at Apple's Lister (for Apple Watch, iOS, and OS X) sample. The sample performs a test for iOS and OS X:

#import <TargetConditionals.h>

#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)

@import ListerKit;

#elif TARGET_OS_MAC

@import ListerKitOSX;

#endif

However, there is no test for TARGET_OS_WATCH or similar. Grepping for watch in TargetConditionals.h delivers no hits:

$ cat /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer
  /SDKs/iPhoneOS7.1.sdk/usr/include/TargetConditionals.h | grep -i watch
$

From TargetConditionals.h, I know there are:

    These conditionals specify in which Operating System the generated code will
    run. The MAC/WIN32/UNIX conditionals are mutually exclusive.  The EMBEDDED/IPHONE 
    conditionals are variants of TARGET_OS_MAC. 

        TARGET_OS_MAC           - Generate code will run under Mac OS
        TARGET_OS_WIN32         - Generate code will run under 32-bit Windows
        TARGET_OS_UNIX          - Generate code will run under some non Mac OS X unix 
        TARGET_OS_EMBEDDED      - Generate code will run under an embedded OS variant
                                  of TARGET_OS_MAC
        TARGET_OS_IPHONE        - Generate code will run under iPhone OS which 
                                  is a variant of TARGET_OS_MAC.
    TARGET_IPHONE_SIMULATOR     - Generate code for running under iPhone Simulator

Question: Is there a preprocessor for Apple's watch?


I'm tagging with , but I'm not sure that's the correct OS for this question.

The list below was compiled from iPhone's TargetConditionals.h. The Simulator and OS X are similar (they just have different bits set to 1):

#define TARGET_OS_MAC               1
#define TARGET_OS_WIN32             0
#define TARGET_OS_UNIX              0
#define TARGET_OS_EMBEDDED          1 
#define TARGET_OS_IPHONE            1 
#define TARGET_IPHONE_SIMULATOR     0 

Questions: Does the watch use TARGET_OS_EMBEDDED? Does the watch omit TARGET_OS_IPHONE?

Answer

Saren Inden picture Saren Inden · Mar 30, 2016

You can find all kind of target conditionals in the TargetConditionals.h (cmd + shift + o and type TargetConditionals.h).

In this list you can find a list like this and many more useful defines. Currently it does contain TARGET_OS_WATCH since WatchOS 2. For WatchOS 1 it was not possible to run custom code on the watch so it was not needed back then since everything ran on the phone itself.

#define TARGET_OS_MAC               1
#define TARGET_OS_WIN32             0
#define TARGET_OS_UNIX              0
#define TARGET_OS_IPHONE            1 
#define TARGET_OS_IOS               0
#define TARGET_OS_WATCH             1
#define TARGET_OS_TV                0
#define TARGET_OS_SIMULATOR         0
#define TARGET_OS_EMBEDDED          1 

Swift Addition

#if os(watchOS)
    [Watch code]
#else
    [Code for iOS, appleTV, or any else clause]
#endif

Some other valid values: iOS, OSX, tvOS

A small explanation about this and more http://nshipster.com/swift-system-version-checking/

At the bottom of this document https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html#//apple_ref/doc/uid/TP40014216-CH8-XID_15#Build Configurations Under the section 'Build Configurations' you can find a (hopefully) up to date list with all these values that are currently available