Conditional Compilation for iPhone
by Warlock on Jun.17, 2009, under Cocoa, iPhone
I’m working on a view right now that I want to be able to cross compile as either an NSView for the Mac, or a UIVIew for the iPhone. I originally wrote it for Leopard, but now I’m trying to add the precompiler directives to let it cross compile between the two platforms. A quick Google search didn’t reveal anything, here are the possible definitions you can use:
#define __MAC_10_0 1000 #define __MAC_10_1 1010 #define __MAC_10_2 1020 #define __MAC_10_3 1030 #define __MAC_10_4 1040 #define __MAC_10_5 1050 #define __MAC_10_6 1060 #define __MAC_NA 9999 /* not available */ #define __IPHONE_2_0 20000 #define __IPHONE_2_1 20100 #define __IPHONE_2_2 20200 #define __IPHONE_3_0 30000 #define __IPHONE_NA 99999 /* not available */
This definition comes from Availability.h located at /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.0.sdk/usr/include/Availability.h (for the iPhone 3.0 SDK).
Obviously, the __MAC_10_x is actually for the version of OSX, and the __IPHONE_x_x is for iPhone.
So, if you wanted to have code conditionally compile for the iPhone (assuming 3.0 SDK) you would do something like the following:
#ifdef __IPHONE_3_0
// iPhone
#import <UIKit/UIKit.h>
#else
// Mac
#import <Cocoa/Cocoa.h>
#endif
February 2nd, 2010 on 4:14 pm
This is not the correct way to do this. Use:
#if TARGET_OS_IPHONE
#import
#else
#import
#endif
TARGET_OS_MAC would be for Mac target.
February 21st, 2010 on 5:21 pm
Thanks David!
I wasn’t able to find a good reference to that back when I was looking.
April 12th, 2010 on 5:01 pm
More info available on stack overflow:
http://stackoverflow.com/questions/146986/what-defines-are-setup-by-xcode-when-compiling-for-iphone
http://stackoverflow.com/questions/458304/how-can-i-programmatically-determine-if-my-app-is-running-in-the-iphone-simulator