views:

985

answers:

3

Hi,

Is there a single preprocessor token that can be used to detect any iPhone device or simulator at build time? I'm currently using:

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

Is this the recommended approach or is there a better way? I'd prefer the macro to be built-in, i.e. defined by the compiler and not by an SDK header file I have to include.

I'm not concerned about distinguishing between iPhone OS versions right now, but if there's an Apple documentation page that details all the relevant macros and when they are and aren't defined then I'd appreciate a link to it as my searching has come up short thus far.

Thanks!

A: 

If you have code that runs 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's also TARGET_IPHONE_SIMULATOR, which is defined only when the app is being built for the simulator. They're still defined in header files, but I hope that helps!

Ben Gotow
+2  A: 

The file you're looking for is TargetConditionals.h, which defines all the macros you're interested in. You'll find it in each version of the SDK, like the following path for the 2.2 SDK:

/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.sdk/usr/include/TargetConditionals.h
Nathan de Vries
Thanks, I hadn't realised initially that TargetConditionals.h was present in Mac OS X SDKs as well as the iPhone SDKs.
richard_nz
+4  A: 

What you want are TARGET_OS_IPHONE and TARGET_IPHONE_SIMULATOR:

#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
//Do iPhone stuff
#else
//Do Mac stuff
#endif
Louis Gerbarg