ios 如何查看iOS版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3339722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check iOS version?
提问by John
I want to check if the iOS
version of the device is greater than 3.1.3
I tried things like:
我想检查iOS
设备的版本是否大于3.1.3
我尝试过的版本:
[[UIDevice currentDevice].systemVersion floatValue]
but it does not work, I just want a:
但它不起作用,我只想要一个:
if (version > 3.1.3) { }
How can I achieve this?
我怎样才能做到这一点?
回答by yasirmturk
/*
* System Versioning Preprocessor Macros
*/
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
/*
* Usage
*/
if (SYSTEM_VERSION_LESS_THAN(@"4.0")) {
...
}
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"3.1.1")) {
...
}
回答by Justin
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.
从 Swift 2.0 开始,您可以使用#available
inif
或guard
来保护应仅在某些系统上运行的代码。
if #available(iOS 9, *) {}
In Objective-C, you need to check the system version and perform a comparison.
if #available(iOS 9, *) {}
在 Objective-C 中,您需要检查系统版本并进行比较。
[[NSProcessInfo processInfo] operatingSystemVersion]
in iOS 8 and above.
[[NSProcessInfo processInfo] operatingSystemVersion]
在 iOS 8 及更高版本中。
As of Xcode 9:
从 Xcode 9 开始:
if (@available(iOS 9, *)) {}
if (@available(iOS 9, *)) {}
The full answer …
完整的答案……
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.
在 Objective-C 和极少数情况下的 Swift 中,最好避免依赖操作系统版本作为设备或操作系统功能的指示。通常有一种更可靠的方法来检查特定功能或类是否可用。
Checking for the presence of APIs:
检查 API 是否存在:
For example, you can check if UIPopoverController
is available on the current device using NSClassFromString
:
例如,您可以使用以下命令检查UIPopoverController
当前设备上是否可用NSClassFromString
:
if (NSClassFromString(@"UIPopoverController")) {
// Do something
}
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:
对于弱链接的类,直接向类发送消息是安全的。值得注意的是,这适用于未明确链接为“必需”的框架。对于缺失的类,表达式计算为 nil,不满足条件:
if ([LAContext class]) {
// Do something
}
Some classes, like CLLocationManager
and UIDevice
, provide methods to check device capabilities:
一些类,如CLLocationManager
和UIDevice
,提供检查设备功能的方法:
if ([CLLocationManager headingAvailable]) {
// Do something
}
Checking for the presence of symbols:
检查符号的存在:
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:
偶尔,您必须检查常量是否存在。这是在 iOS 8 中引入的UIApplicationOpenSettingsURLString
,用于通过-openURL:
. 该值在 iOS 8 之前不存在。将 nil 传递给此 API 会崩溃,因此您必须先验证常量是否存在:
if (&UIApplicationOpenSettingsURLString != NULL) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
Comparing against the operating system version:
与操作系统版本对比:
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:
假设您需要检查操作系统版本的情况相对较少。对于针对 iOS 8 及更高NSProcessInfo
版本的项目,包括一种执行版本比较的方法,错误几率较小:
- (BOOL)isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion)version
Projects targeting older systems can use systemVersion
on UIDevice
. Apple uses it in their GLSpritesample code.
针对旧系统的项目可以systemVersion
在UIDevice
. Apple 在他们的GLSprite示例代码中使用它。
// 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;
}
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).
如果出于某种原因您决定这systemVersion
是您想要的,请确保将其视为字符串,否则您可能会截断补丁修订号(例如 3.1.2 -> 3.1)。
回答by CarlJ
As suggested by the official Apple docs: you can use the NSFoundationVersionNumber
, from the NSObjCRuntime.h
header file.
正如建议苹果官方文档:您可以使用NSFoundationVersionNumber
,从NSObjCRuntime.h
头文件。
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
// here you go with iOS 7
}
回答by C?ur
Starting Xcode 9, in Objective-C:
在Objective-C 中启动 Xcode 9 :
if (@available(iOS 11, *)) {
// iOS 11 (or newer) ObjC code
} else {
// iOS 10 or older code
}
Starting Xcode 7, in Swift:
在Swift 中启动 Xcode 7 :
if #available(iOS 11, *) {
// iOS 11 (or newer) Swift code
} else {
// iOS 10 or older code
}
For the version, you can specify the MAJOR, the MINOR or the PATCH (see http://semver.org/for definitions). Examples:
对于版本,您可以指定 MAJOR、MINOR 或 PATCH(有关定义,请参见http://semver.org/)。例子:
iOS 11
andiOS 11.0
are the same minimal versioniOS 10
,iOS 10.3
,iOS 10.3.1
are different minimal versions
iOS 11
并且iOS 11.0
是相同的最小版本iOS 10
,iOS 10.3
,iOS 10.3.1
是不同的最小版本
You can input values for any of those systems:
您可以为这些系统中的任何一个输入值:
iOS
,macOS
,watchOS
,tvOS
iOS
,macOS
,watchOS
,tvOS
Real case example taken from one of my pods:
取自我的一个豆荚的真实案例示例:
if #available(iOS 10.0, tvOS 10.0, *) {
// iOS 10+ and tvOS 10+ Swift code
} else {
// iOS 9 and tvOS 9 older code
}
回答by Travis M.
This is used to check for compatible SDK version in Xcode, this is if you have a large team with different versions of Xcode or multiple projects supporting different SDKs that share the same code:
这用于检查 Xcode 中兼容的 SDK 版本,如果您有一个拥有不同 Xcode 版本的大型团队或多个项目支持共享相同代码的不同 SDK:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
//programming in iOS 8+ SDK here
#else
//programming in lower than iOS 8 here
#endif
What you really want is to check the iOS version on the device. You can do that with this:
您真正想要的是检查设备上的 iOS 版本。你可以这样做:
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
//older than iOS 8 code here
} else {
//iOS 8 specific code here
}
Swift version:
迅捷版:
if let version = Float(UIDevice.current.systemVersion), version < 9.3 {
//add lower than 9.3 code here
} else {
//add 9.3 and above code here
}
Current versions of swift should be using this:
当前版本的 swift 应该使用这个:
if #available(iOS 12, *) {
//iOS 12 specific code here
} else {
//older than iOS 12 code here
}
回答by Jonathan Grynspan
Try:
尝试:
NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: @"3.1.3" options: NSNumericSearch];
if (order == NSOrderedSame || order == NSOrderedDescending) {
// OS version >= 3.1.3
} else {
// OS version < 3.1.3
}
回答by Daniel Galasko
Preferred Approach
首选方法
In Swift 2.0 Apple added availability checking using a far more convenient syntax (Read more here). Now you can check the OS version with a cleaner syntax:
在 Swift 2.0 中,Apple 使用更方便的语法添加了可用性检查(在这里阅读更多)。现在您可以使用更简洁的语法检查操作系统版本:
if #available(iOS 9, *) {
// Then we are on iOS 9
} else {
// iOS 8 or earlier
}
This is the preferred over checking respondsToSelector
etc (What's New In Swift). Now the compiler will always warn you if you aren't guarding your code properly.
这是首选的检查respondsToSelector
等(Swift 中的新功能)。现在,如果您没有正确保护代码,编译器将始终警告您。
Pre Swift 2.0
Swift 2.0 之前的版本
New in iOS 8 is NSProcessInfo
allowing for better semantic versioning checks.
iOS 8 中的新功能NSProcessInfo
允许进行更好的语义版本控制检查。
Deploying on iOS 8 and greater
在 iOS 8 及更高版本上部署
For minimum deployment targets of iOS 8.0or above, use
NSProcessInfo
operatingSystemVersion
orisOperatingSystemAtLeastVersion
.
对于iOS 8.0或更高版本的最低部署目标,请使用
NSProcessInfo
operatingSystemVersion
或isOperatingSystemAtLeastVersion
。
This would yield the following:
这将产生以下结果:
let minimumVersion = NSOperatingSystemVersion(majorVersion: 8, minorVersion: 1, patchVersion: 2)
if NSProcessInfo().isOperatingSystemAtLeastVersion(minimumVersion) {
//current version is >= (8.1.2)
} else {
//current version is < (8.1.2)
}
Deploying on iOS 7
在 iOS 7 上部署
For minimum deployment targets of iOS 7.1or below, use compare with
NSStringCompareOptions.NumericSearch
onUIDevice systemVersion
.
对于iOS 7.1或以下的最低部署目标,请使用 compare 与
NSStringCompareOptions.NumericSearch
onUIDevice systemVersion
。
This would yield:
这将产生:
let minimumVersionString = "3.1.3"
let versionComparison = UIDevice.currentDevice().systemVersion.compare(minimumVersionString, options: .NumericSearch)
switch versionComparison {
case .OrderedSame, .OrderedDescending:
//current version is >= (3.1.3)
break
case .OrderedAscending:
//current version is < (3.1.3)
fallthrough
default:
break;
}
More reading at NSHipster.
更多阅读NSHipster。
回答by Segev
I always keep those in my Constants.h file:
我总是将它们保存在我的 Constants.h 文件中:
#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)
#define IS_OS_5_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0)
#define IS_OS_6_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
#define IS_OS_7_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
回答by Takahiko Kawasaki
With Versionclass that is contained in nv-ios-versionproject (Apache License, Version 2.0), it is easy to get and compare iOS version. An example code below dumps the iOS version and checks whether the version is greater than or equal to 6.0.
随着版本包含在类NV-IOS版本项目(Apache许可证2.0版),很容易得到和比较的iOS版本。下面的示例代码转储 iOS 版本并检查版本是否大于或等于 6.0。
// Get the system version of iOS at runtime.
NSString *versionString = [[UIDevice currentDevice] systemVersion];
// Convert the version string to a Version instance.
Version *version = [Version versionWithString:versionString];
// Dump the major, minor and micro version numbers.
NSLog(@"version = [%d, %d, %d]",
version.major, version.minor, version.micro);
// Check whether the version is greater than or equal to 6.0.
if ([version isGreaterThanOrEqualToMajor:6 minor:0])
{
// The iOS version is greater than or equal to 6.0.
}
// Another way to check whether iOS version is
// greater than or equal to 6.0.
if (6 <= version.major)
{
// The iOS version is greater than or equal to 6.0.
}
Project Page:nv-ios-version
TakahikoKawasaki/nv-ios-version
项目页面:nv-ios-version
TakahikoKawasaki/nv-ios-version
Blog:Get and compare iOS version at runtime with Version class
Get and compare iOS version at runtime with Version class
博客:使用 Version 类获取并比较运行时的 iOS 版本 使用 Version 类
获取并比较运行时的 iOS 版本
回答by Jef
+(BOOL)doesSystemVersionMeetRequirement:(NSString *)minRequirement{
// eg NSString *reqSysVer = @"4.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:minRequirement options:NSNumericSearch] != NSOrderedAscending)
{
return YES;
}else{
return NO;
}
}