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

IosXcodeMacosConditional Compilation

Ios Problem Overview

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.

Ios Solutions

Solution 1 - Ios

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


Solution 2 - Ios

To look at all the defined macros, add this to the "Other C Flags" of your build config:

-g3 -save-temps -dD

You will get some build errors, but the compiler will dump all the defines into .mi files in your project's root directory. You can use grep to look at them, for example:

grep define main.mi 

When you're done, don't forget to remove these options from the build setting.

Solution 3 - Ios

The answers to this question aren't quite correct. The question of the platform and hardware vs Simulator is orthogonal.

Do not use architecture as a shortcut for platform or simulator detection! This kind of sloppy thinking has caused many, many programmers a great deal of heartburn and headache over the years.

Here is an ASCII chart of the conditionals. The names don't necessarily make sense for historical reasons:

|             TARGET_OS_MAC            |
| +---+  +---------------------------+ |
| |   |  |      TARGET_OS_IPHONE     | |
| |OSX|  | +-----+ +----+ +-------+  | |
| |   |  | | IOS | | TV | | WATCH |  | |
| |   |  | +-----+ +----+ +-------+  | |
| +---+  +---------------------------+ |


TARGET_OS_MAC is true for all Apple platforms.

TARGET_OS_OSX is true only for macOS

TARGET_OS_IPHONE is true for iOS, watchOS, and tvOS (devices & simulators)

TARGET_OS_IOS is true only for iOS (devices & simulators)

TARGET_OS_WATCH is true only for watchOS (devices & simulators)

TARGET_OS_TV is true only for tvOS (devices & simulators)

TARGET_OS_EMBEDDED is true only for iOS/watchOS/tvOS hardware

TARGET_OS_SIMULATOR is true only for the Simulator.

I'll also note that you can conditionalize settings in xcconfig files by platform:

//macOS only
SOME_SETTING[sdk=macosx] = ...

//iOS (device & simulator)
SOME_SETTING[sdk=iphone*] = ...
//iOS (device)
SOME_SETTING[sdk=iphoneos*] = ...
//iOS (simulator)
SOME_SETTING[sdk=iphonesimulator*] = ...

//watchOS (device & simulator)
SOME_SETTING[sdk=watch*] = ...
//watchOS (device)
SOME_SETTING[sdk=watchos*] = ...
//watchOS (simulator)
SOME_SETTING[sdk=watchsimulator*] = ...

//tvOS (device & simulator)
SOME_SETTING[sdk=appletv*] = ...
//tvOS (device)
SOME_SETTING[sdk=appletvos*] = ...
//tvOS (simulator)
SOME_SETTING[sdk=appletvsimulator*] = ...

// iOS, tvOS, or watchOS Simulator
SOME_SETTING[sdk=embeddedsimulator*] = ...


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAirsource LtdView Question on Stackoverflow
Solution 1 - IosAirsource LtdView Answer on Stackoverflow
Solution 2 - IoslajosView Answer on Stackoverflow
Solution 3 - IosrussbishopView Answer on Stackoverflow