ios11中的NSFoundationVersionNumber—如何以编程方式检测iOS版本

时间:2022-12-05 00:20:15

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

在之前的iOS版本中,我使用NSFoundationVersionNumber检测iOS版本:

#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.

现在我想对ios11做同样的事情,但是我找不到正确的NSFoundationVersionNumber。文档显示,最新的是NSFoundationVersionNumber_iOS_9_x_Max。

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

那么,如何使用正确的NSFoundationVersionNumber来检测ios11是否被使用呢?

3 个解决方案

#1


13  

One of the designated ways to get system version is with the systemVersion property of UIDevice. https://developer.apple.com/documentation/uikit/uidevice/1620043-systemversion?language=objc

获取系统版本的指定方法之一是使用UIDevice的systemVersion属性。https://developer.apple.com/documentation/uikit/uidevice/1620043-systemversion?language=objc

NSString *systemVersion = [[UIDevice currentDevice] systemVersion];

NSString *systemVersion = [[UIDevice currentDevice] systemVersion];

You can use it this way:

你可以这样使用:

#define IS_IOS11orHIGHER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 11.0)

#2


64  

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

如果您正在使用Xcode 9或更好的,您可以使用@available属性:

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

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

在Swift中,它是相同的,但是拼写为#available而不是@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.

这是一个比其他方法更好的方法,因为编译器知道它,并且会适当地抑制可用性警告。因此,在上面的示例中,您可以在if块中使用ios11.0中引入的api,而不必担心项目的部署目标无法使用这些api。

#3


1  

Apple NSProcessInfo

苹果NSProcessInfo

Apple now provides the NSOperatingSystemVersion struct in NSProcessInfo

苹果现在在NSProcessInfo中提供了NSOperatingSystemVersion struct

You can use it with the isOperatingSystemAtLeastVersion:, or just check the 3 fields to exactly know which system version is running your app.

您可以使用它与isOperatingSystemAtLeastVersion:,或只是检查3个字段,以确切地知道哪个系统版本正在运行您的应用程序。

It is available in both obj c and swift

它可以在obj c和swift中使用

#1


13  

One of the designated ways to get system version is with the systemVersion property of UIDevice. https://developer.apple.com/documentation/uikit/uidevice/1620043-systemversion?language=objc

获取系统版本的指定方法之一是使用UIDevice的systemVersion属性。https://developer.apple.com/documentation/uikit/uidevice/1620043-systemversion?language=objc

NSString *systemVersion = [[UIDevice currentDevice] systemVersion];

NSString *systemVersion = [[UIDevice currentDevice] systemVersion];

You can use it this way:

你可以这样使用:

#define IS_IOS11orHIGHER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 11.0)

#2


64  

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

如果您正在使用Xcode 9或更好的,您可以使用@available属性:

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

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

在Swift中,它是相同的,但是拼写为#available而不是@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.

这是一个比其他方法更好的方法,因为编译器知道它,并且会适当地抑制可用性警告。因此,在上面的示例中,您可以在if块中使用ios11.0中引入的api,而不必担心项目的部署目标无法使用这些api。

#3


1  

Apple NSProcessInfo

苹果NSProcessInfo

Apple now provides the NSOperatingSystemVersion struct in NSProcessInfo

苹果现在在NSProcessInfo中提供了NSOperatingSystemVersion struct

You can use it with the isOperatingSystemAtLeastVersion:, or just check the 3 fields to exactly know which system version is running your app.

您可以使用它与isOperatingSystemAtLeastVersion:,或只是检查3个字段,以确切地知道哪个系统版本正在运行您的应用程序。

It is available in both obj c and swift

它可以在obj c和swift中使用