Search code examples
objective-cconditional-compilation

How do I check the iOS target version in Objective-C


I am currently working on an iOS app that targets iOS 9. Due to the age of this target version, I must work with older APIs and sometimes have to add workarounds for bugs in older versions.

I would like to make my code a bit easier to maintain, and preemptively mark parts that need to be refactored once the target version changes. Right now I am using TODO comments, but #warning directives would be nicer as they are better visible.

Is there a way to use an #if or #ifdef directive to check for the iOS target version of the app so that the warning will only appear once the target version changes?


Solution

  • You can check the value of the __IPHONE_OS_VERSION_MIN_REQUIRED preprocessor macro:

    #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 100000
    #endif
    

    That literal 100000 is the value of __IPHONE_10_0 from <Availability.h> but, as the comment there recommends, you should use the literal value, not the symbolic constant.