Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSFoundationVersionNumber in iOS 11 - How to detect iOS version programmatically

In previous iOS versions I used NSFoundationVersionNumber to detect the iOS version:

#define IS_IOS10orHIGHER (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_9_x_Max)
#define IS_IOS9orHIGHER (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_8_3)
#define IS_IOS8orHIGHER (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1)
#define IS_IOS7orHIGHER (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
#define IS_IOS6orHIGHER (floor(NSFoundationVersionNumber) >= NSFoundationVersionNumber_iOS_6_0)
...

Now I would to do the same for iOS 11, but I was not able to find the correct NSFoundationVersionNumber. The docs show NSFoundationVersionNumber_iOS_9_x_Max to be the latest.

So, how is the correct NSFoundationVersionNumber to detect if iOS 11 is used?

like image 275
Andrei Herford Avatar asked Aug 23 '17 14:08

Andrei Herford


People also ask

How do I find my OS version in Objective C?

In Objective-C, you need to check the system version and perform a comparison. [[NSProcessInfo processInfo] operatingSystemVersion] in iOS 8 and above.

How do I find my swift version OS?

Android DevicesTouch "Settings," then touch "About Phone" or "About Device." From there, you can find the Android version of your device.


1 Answers

If you are using Xcode 9 or better, you can use the @available attribute:

if (@available(iOS 11.0, *)) {
    NSLog(@"newest");
} else {
    NSLog(@"not newest");
}

In Swift, it's the same, but spelled #available instead of @available.

The reason that this is a better way to do this than the other methods is because the compiler is aware of it, and will suppress availability warnings appropriately. So in the example above, you can use APIs that were introduced in iOS 11.0 within the if block without being warned about those APIs being unavailable to your project's deployment target.

like image 157
Charles Srstka Avatar answered Oct 06 '22 20:10

Charles Srstka