views:

2128

answers:

3

I would like to conditionally include code for an iPhone app depending on which version of the SDK I'm compiling against. On Mac OS X, there is the MAC_OS_X_VERSION_MIN_REQUIRED preprocessor macro which gets set to the value of the MACOSX_DEPLOYMENT_TARGET build setting by the compiler. Is there an equivalent on the iPhone?

Update:

I've set IPHONE_DEPLOYMENT_TARGET to 3.0 in the build settings, but Xcode is passing -D__IPHONE_OS_VERSION_MIN_REQUIRED=20000 and -mmacosx-version-min=10.5 to GCC. Shouldn't the first one be 30000 and the second one be -miphoneos-version-min=3.0? What am I doing wrong?

Update 2:

Looks like I wasn't doing anything wrong. __IPHONE_OS_VERSION_MIN_REQUIRED and -miphoneos-version-min are both set correctly when building for a device -- it's only wrong when using the iPhone Simulator SDK. I think it's a bug in the simulator SDK.

+1  A: 

For what it's worth, a decent work-around if such a macro does not exist, is to create 2 build targets, and in one of them add the build setting GCC_PREPROCESSOR_DEFINITIONS with a value like IPHONE_OS_3. Then in your code you can do:

#ifdef IPHONE_OS_3
    [foo thisMethodIsUnderNDA];
#else
    [foo oldSchoolMethod];
#endif
Daniel Dickison
+3  A: 

There are preprocessor macros that are defined for each version of the OS. For example, if __IPHONE_OS_3_0 is defined, then you're building against the 3.0 SDK (or possibly later, I'm not certain).

Brent Royal-Gordon
That should be __IPHONE_3_0 (note: no _OS_), but I was not aware of that. Thanks. It would be nice if I could use __IPHONE_OS_VERSION_MIN_REQUIRED so it's upwards compatible, but I'm having Xcode problems with that (see update).
Daniel Dickison
+9  A: 

See Availability.h

    #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_2_0

etc.

http://developer.apple.com/iphone/library/documentation/DeveloperTools/Reference/XcodeBuildSettingRef/1-Build_Setting_Reference/build_setting_ref.html#//apple_ref/doc/uid/TP40003931-CH3-SW156

cdespinosa
For some reason Xcode is giving GCC -D__IPHONE_OS_VERSION_MIN_REQUIRED=20000 even though I've set the iPhone Deployment Target to 3.0. It's also passing -mmacosx-version-min=10.5 instead of -miphoneos-version-min=3.0, so something looks broken. Any ideas?
Daniel Dickison