Built-in preprocessor token for iPhone platform discovery

Is there one preprocessor token that can be used to detect any iPhone device or simulator during build? I am currently using:

#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED // This is an iPhone build #endif 

Is this a recommended approach or is there a better way? I would prefer the macro to be inline, i.e. Defined by the compiler, not the SDK header file that I have to include.

I'm not worried about distinguishing between versions of the iPhone OS right now, but if there is an Apple documentation page that lists all the relevant macros, and when they are not defined, I would rate the link to it as my search is still inactive.

Thanks!

+6
preprocessor iphone
source share
3 answers

The file you are looking for is TargetConditionals.h , which defines all the macros that interest you. You will find it in each version of the SDK, for example, the following path for 2.2 SDK:

 /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.sdk/usr/include/TargetConditionals.h 
+8
source share

From this site we find that you need TARGET_OS_IPHONE

 #if TARGET_OS_IPHONE //Do iPhone stuff #else //Do Mac stuff #endif 
+13
source share

If you have code that works on the iPhone and on the desktop, you can use TARGET_OS_IPHONE to determine if the target OS is any version of the iPhone OS. There is also TARGET_IPHONE_SIMULATOR , which is determined only when the application is created for the simulator. They are still defined in the header files, but I hope this helps!

+2
source share

All Articles