如何查看iOS版本? [英] How to check iOS version?

查看:112
本文介绍了如何查看iOS版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查设备的 iOS 版本是否大于 3.1.3
我尝试过这样的事情:

I want to check if the iOS version of the device is greater than 3.1.3 I tried things like:

[[UIDevice currentDevice].systemVersion floatValue]

但它不起作用,我只想要一个:

but it does not work, I just want a:

if (version > 3.1.3) { }

我如何实现这一目标?

推荐答案

快速回答......





从Swift 2.0开始,您可以在中使用 #available ,如果后卫保护只应在某些系统上运行的代码。

The quick answer …


As of Swift 2.0, you can use #available in an if or guard to protect code that should only be run on certain systems.

if #available(iOS 9,*){}




$
在Objective-C中,您需要检查系统版本并进行比较。

if #available(iOS 9, *) {}


In Objective-C, you need to check the system version and perform a comparison.

iOS 8及更高版本中的[[NSProcessInfo processInfo] operatingSystemVersion]

从Xcode 9开始:

As of Xcode 9:

if(@available (iOS 9,*)){}


在Objective-C和Swift中,在极少数情况下,最好避免依赖操作系统版本作为设备或操作系统功能的指示。通常有一种更可靠的检查特定功能或类是否可用的方法。

In Objective-C, and Swift in rare cases, it's better to avoid relying on the operating system version as an indication of device or OS capabilities. There is usually a more reliable method of checking whether a particular feature or class is available.

检查是否存在API:

例如,您可以使用 NSClassFromString UIPopoverController c $ c>:

For example, you can check if UIPopoverController is available on the current device using NSClassFromString:

if (NSClassFromString(@"UIPopoverController")) {
    // Do something
}

对于弱链接类,直接向类消息是安全的。值得注意的是,这适用于未明确链接为必需的框架。对于缺少的类,表达式的计算结果为nil,条件失败:

For weakly linked classes, it is safe to message the class, directly. Notably, this works for frameworks that aren't explicitly linked as "Required". For missing classes, the expression evaluates to nil, failing the condition:

if ([LAContext class]) {
    // Do something
}

某些类,如 CLLocationManager UIDevice ,提供检查设备功能的方法:

Some classes, like CLLocationManager and UIDevice, provide methods to check device capabilities:

if ([CLLocationManager headingAvailable]) {
    // Do something
}

检查是否存在符号:

非常偶然,您必须检查是否存在常量。这在iOS 8中引入了 UIApplicationOpenSettingsURLString ,用于通过 -openURL:加载设置应用。在iOS 8之前该值不存在。将nil传递给此API将崩溃,因此您必须首先检查是否存在常量:

Very occasionally, you must check for the presence of a constant. This came up in iOS 8 with the introduction of UIApplicationOpenSettingsURLString, used to load Settings app via -openURL:. The value didn't exist prior to iOS 8. Passing nil to this API will crash, so you must take care to verify the existence of the constant first:

if (&UIApplicationOpenSettingsURLString != NULL) {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}

与操作系统版本进行比较:

让我们假设您面临检查操作系统版本的相对罕见的需求。对于面向iOS 8及更高版本的项目, NSProcessInfo 包含一种执行版本比较的方法,错误机会较少:

Let's assume you're faced with the relatively rare need to check the operating system version. For projects targeting iOS 8 and above, NSProcessInfo includes a method for performing version comparisons with less chance of error:

- (BOOL)isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion)version

项目定位旧系统可以在 UIDevice 上使用 systemVersion 。 Apple在其 GLSprite 示例代码中使用它。

Projects targeting older systems can use systemVersion on UIDevice. Apple uses it in their GLSprite sample code.

// A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
// class is used as fallback when it isn't available.
NSString *reqSysVer = @"3.1";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending) {
    displayLinkSupported = TRUE;
}

如果出于某种原因你决定 systemVersion 是您想要的,请确保将其视为字符串,否则您可能会截断修补程序版本号(例如3.1.2 - > 3.1)。

If for whatever reason you decide that systemVersion is what you want, make sure to treat it as an string or you risk truncating the patch revision number (eg. 3.1.2 -> 3.1).

这篇关于如何查看iOS版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆