5

In the past I used the following preprocessor code to conditionally execute code for different iOS versions:

#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
// target is iOS
    #if __IPHONE_OS_VERSION_MIN_REQUIRED < 60000
    // target is lower than iOS 6.0
    #else
    // target is at least iOS 6.0
    #endif
#endif

However with iOS 7 I have the following problem:

#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
// target is iOS
    #if __IPHONE_OS_VERSION_MIN_REQUIRED < 70000
    // target is lower than iOS 7.0
    NSLog(@"This message should only appear if iOS version is 6.x or lower");
    #else
    // target is at least iOS 7.0
    #endif
#endif

The NSLog message above appears on console under iOS 7. Am I doing something wrong?

EDIT: The following code running under iOS 7 (simulator and device)

NSLog(@"Version %i", __IPHONE_OS_VERSION_MIN_REQUIRED);

gives: Version 60000

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
FrankZp
  • 2,022
  • 3
  • 28
  • 41

2 Answers2

8

That is the Deployment Target of your app (the minimum version where your app can be installed), not the version where the app is running in the device.

In the settings of your project, you can set that field:

enter image description here

If you change it like this, this input:

NSLog(@"Version %i", __IPHONE_OS_VERSION_MIN_REQUIRED);

Returns 7000

If what you want is to check the actual version of the operative system, I refer you to this question:

How to check iOS version?

But, it's done in runtime, not at compile time.

Community
  • 1
  • 1
Antonio MG
  • 20,382
  • 3
  • 43
  • 62
  • Thank you. This one is also a great addition to the post you have linked: http://stackoverflow.com/a/7848772/1685971 – FrankZp Sep 20 '13 at 13:18
1
  #ifdef __AVAILABILITY_INTERNAL__IPHONE_9_0_DEP__IPHONE_9_0
    // you're in Xcode 7.x and can use buggy SDK with ios 9.0 only functionality
        CGFloat iOSVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (iOSVersion>=9) {
        // same good old API that predates brave new post Steve Jobs world of bugs and crashes
    }
  #else
        // you're running Xcode 6.4 or older and should use older API here
  #endif

swift:

    if #available(iOS 13, *) {
        toSearchBar?.isHidden = true
    } else {
        // a path way to discovering how fast UIKit will rot
        // now that there is SwiftUI
    }
Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66