What #defines are set up by Xcode when compiling for iPhone

Airsource Ltd picture Airsource Ltd · Sep 29, 2008 · Viewed 32.8k times · Source

I'm writing some semi-portable code and want to be able to detect when I'm compiling for iPhone. So I want something like #ifdef IPHONE_SDK....

Presumably Xcode defines something, but I can't see anything under project properties, and Google isn't much help.

Answer

Airsource Ltd picture Airsource Ltd · Sep 29, 2008

It's in the SDK docs under "Compiling source code conditionally"

The relevant definitions are TARGET_OS_IPHONE (and he deprecated TARGET_IPHONE_SIMULATOR), which are defined in /usr/include/TargetConditionals.h within the iOS framework. On earlier versions of the toolchain, you had to write:

#include "TargetConditionals.h"

but this is no longer necessary on the current (xCode 6/iOS8) toolchain.

So, for example, if you want to only compile a block of code if you are building for the device, then you should do

#if !(TARGET_OS_SIMULATOR)
...
#endif