Ios – __IPHONE_OS_VERSION_MIN_REQUIRED does not return deployment target

backwards-compatibilityiosiphonexcode

Why does __IPHONE_OS_VERSION_MIN_REQUIRED return the base SDK instead of the deployment target?

I want to use a class that can only run on iOS 4.3 and greater, but still support 4.0 and greater. To achieve this, I assert if I try to use this class on devices with an iOS version lower than 4.3. To avoid asserting, I avoid the class in-code by checking for the availability of the 4.3 methods. The deployment target is currently set to 4.0.

However, because the asserts would only happen when I run the application on an old device, I also want to add a warning if the deployment target is less than 4.3. I am trying to use __IPHONE_OS_VERSION_MIN_REQUIRED. However, this somehow keeps returning 50000 (the base SDK) instead of something below 43000 and I can't figure out why.

Code:

NSLog(@"Deployment target: %i", __IPHONE_OS_VERSION_MIN_REQUIRED); // Returns 50000 instead of 40000.
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 43000
// Never gets here
NSLog(@"%@", @"WARNING! NWMethodWrapper does not work on iOS versions below 4.3. If you insist on supporting older iOS versions, make sure you do not call NWMethodWrapper methods unless dlsym(RTLD_DEFAULT,\"imp_implementationWithBlock\") evaluates to true.");
#endif
NSAssert(dlsym(RTLD_DEFAULT,"imp_implementationWithBlock"), @"NWMethodWrapper uses methods that are only available from iOS 4.3 and later."); // Asserts, as appropriate (running on iOS 4.2.1).

Edit: My deployment target is already set to 4.0, which is why I'm asking the question.

Best Answer

If you don't define __IPHONE_OS_VERSION_MIN_REQUIRED yourself, __IPHONE_OS_VERSION_MIN_REQUIRED is ultimately set by the compiler (via a Macro in "AvailabilityInternal.h") to match what you have your minimum iphone OS version set to, so you need to make sure your deployment target is set for something earlier than iOS 5.0.

Related Topic