What #defines are set up by Xcode when compiling for iPhone
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
Presumably Xcode defines something, but I can’t see anything under project properties, and Google isn’t much help.
- Which conditional compile to use to switch between Mac and iPhone specific code?
- When compiling for multiple targets in XCode, how do i ensure that certain files will not be included one target
- Swift: iOS Deployment Target Command Line Flag
- How do I write universal swift code for both iOS and OS X. In cocoa I could use #ifdef, what do I do now?
- How to compile specific files in objective-c++ and the rest of the project in objective-c
- Conditional Compilation - Check Scheme
3 Solutions Collect From Internet About “What #defines are set up by Xcode when compiling for iPhone”
It’s in the SDK docs under “Compiling source code conditionally”
The relevant definitions are TARGET_OS_IPHONE and 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:
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_IPHONE_SIMULATOR) ... #endif
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.
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 | | | | | | | +-----+ +----+ +-------+ | | | +---+ +---------------------------+ | +--------------------------------------+ Devices: TARGET_OS_EMBEDDED Simulators: TARGET_OS_SIMULATOR
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*] = ...
- Is it possible to stub HTTP requests in Xcode 7 automated UI tests?
- iOS call log access api
- Integrate iAd pre-roll video integration in my app?
- Face Detection on iPhone using OpenCV and LBP
- “_OBJC_CLASS_$_”, referenced from: error in xcode 4.3.2
- Embedding YouTube videos on
- A Swift example of Custom Views for Data Input (custom in-app keyboard)
- How can I convert FBProfilePictureView to an UIImage?
- iOS and bluetooth
- UISearchController causes black screen Swift 2.0
- Categories in Objective-C aren't working
- UIViews ending up beneath tab bar
- Build Static Library in XCode 4.6 for Simulator
- Objective-c/iOS – Open local html file with Safari
- Convert a two byte UInt8 array to a UInt16 in Swift