我想检查设备的iOS版本是否大于3.1.3
。我尝试了以下方法:
[[UIDevice currentDevice].systemVersion floatValue]
但是它不起作用,我只想要一个:
if (version > 3.1.3) { }
我该如何实现这个?
我想检查设备的iOS版本是否大于3.1.3
。我尝试了以下方法:
[[UIDevice currentDevice].systemVersion floatValue]
但是它不起作用,我只想要一个:
if (version > 3.1.3) { }
我该如何实现这个?
/*
* 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")) {
...
}
从Swift 2.0开始,您可以在if
或guard
中使用#available
来保护只应在某些系统上运行的代码。
if #available(iOS 9, *) {}
在Objective-C中,您需要检查系统版本并执行比较。
[[NSProcessInfo processInfo] operatingSystemVersion]
在iOS 8及以上版本中。
从Xcode 9开始:
if (@available(iOS 9, *)) {}
在Objective-C和Swift中很少情况下,最好不要依赖操作系统版本作为设备或操作系统功能的指示。通常有更可靠的方法来检查特定功能或类是否可用。
检查API的存在:
例如,您可以使用NSClassFromString
检查当前设备上是否有UIPopoverController
:
if (NSClassFromString(@"UIPopoverController")) {
// Do something
}
if ([LAContext class]) {
// Do something
}
一些类(如CLLocationManager
和UIDevice
)提供了检查设备功能的方法:
if ([CLLocationManager headingAvailable]) {
// Do something
}
检查符号的存在:
很少情况下,您需要检查常量是否存在。这在iOS 8中引入了UIApplicationOpenSettingsURLString
时出现,用于通过-openURL:
加载设置应用程序。该值在iOS 8之前不存在。将nil传递给此API会导致崩溃,因此您必须先验证常量的存在:
if (&UIApplicationOpenSettingsURLString != NULL) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
与操作系统版本进行比较:
假设您面临着相对罕见的需要检查操作系统版本的情况。对于针对iOS 8及以上版本的项目,NSProcessInfo
包括一种执行版本比较的方法,减少出错的可能性:
- (BOOL)isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion)version
针对旧系统的项目可以使用 UIDevice
上的 systemVersion
。苹果在他们的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;
}
如果出于任何原因您决定使用systemVersion
,请确保将其视为字符串,否则您有可能会截断补丁修订号(例如3.1.2 -> 3.1)。
UISplitViewController
中发送-viewWillAppear
。我的解决方法是确定是否小于iOS 4.0,并在Root View的-didSelectRowAtIndexPath
中自己将其发送到Detail View Controller。 - jww[@"10.0" compare:@"10" options:NSNumericSearch]
返回的是NSOrderedDescending
,这可能并不是预期的结果。(我本来可能希望结果是NSOrderedSame
) 至少从理论上讲,这是有可能发生的。 - KenoperatingSystemVersionString
的方法。 - dcowNSFoundationVersionNumber
,只有在不可能时才应该检查UIDevice
上的系统版本。 - rckoenes根据官方Apple文档的建议:您可以使用来自NSObjCRuntime.h
头文件的NSFoundationVersionNumber
。
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
// here you go with iOS 7
}
NSFoundationVersionNumber_iOS_6_1
。 - Kjuly#ifndef NSFoundationVersionNumber_iOS_7_0 #define NSFoundationVersionNumber_iOS_7_0 1047.00 #endif
来定义它。为了避免混乱,我选择了来自yasirmturk的SYSTEM_VERSION_LESS_THAN(v)宏。 - CœurNSFoundationVersionNumber_iOS_8_0
。只需检查NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1
即可。 - CarlJif (@available(iOS 11, *)) {
// iOS 11 (or newer) ObjC code
} else {
// iOS 10 or older code
}
if #available(iOS 11, *) {
// iOS 11 (or newer) Swift code
} else {
// iOS 10 or older code
}
关于版本,您可以指定主要版本、次要版本或修订版本(请参考http://semver.org/了解定义)。例如:
iOS 11
和iOS 11.0
是相同的最低版本iOS 10
、iOS 10.3
、iOS 10.3.1
是不同的最低版本您可以为任何这些系统输入值:
iOS
、macOS
、watchOS
、tvOS
、visionOS
,等等(请参考文档)这是一个真实案例,取自我的其中一个库:
if #available(iOS 10.0, tvOS 10.0, *) {
// iOS 10+ and tvOS 10+ Swift code
} else {
// iOS 9 and tvOS 9 older code
}
guard
代替在else
语句中保持块的打开和缩进吗? - Legolessif
块,然后跟一个 else
。 - Cœurif
块。 - Legolessif (@available(iOS 11, *))
这段代码是对 iOS 11 版本进行运行时检查还是编译时检查,还是两者都包含? - Jan尝试:
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
}
if ([UIDevice currentDevice].systemVersion compare:@"3.1.3" options:NSNumericSearch] == NSOrderedAscending) ;// OS version < 3.1.3 else ;// OS version >= 3.1.3
- Rob在 Swift 2.0 中,苹果公司使用更加方便的语法添加了可用性检查(阅读更多信息)。现在,您可以使用更简洁的语法检查操作系统版本:
if #available(iOS 9, *) {
// Then we are on iOS 9
} else {
// iOS 8 or earlier
}
根据Swift新特性,检查respondsToSelector
不如采用这种方式。现在,编译器会始终提醒您是否正确保护了代码。
iOS 8中新增了NSProcessInfo
,使得语义版本检查更加精确。
对于最低部署目标为iOS 8.0或更高版本,请使用
NSProcessInfo
的operatingSystemVersion
或isOperatingSystemAtLeastVersion
。
这将产生以下结果:
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)
}
对于最低部署目标是iOS 7.1或以下的情况,可以使用比较符号与
UIDevice systemVersion
一起使用NSStringCompareOptions.NumericSearch
。
这将产生以下结果:
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;
}
更多阅读请访问 NSHipster。
我总是将以下内容保存在我的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)
.pch
文件中。 - Vaibhav JhaveriConstants.h
文件中,并在我的 pch
文件中导入。 - Segev+(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;
}
}
Availability.h中的编译时宏。这里的所有其他方法都是运行时解决方案,不适用于标头文件、类别或实例变量定义。
对于这些情况,请使用
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0 && defined(__IPHONE_14_0)
// iOS 14+ code here
#else
// Pre iOS 14 code here
#endif
转载自这个答案
使用包含在nv-ios-version项目中的Version类(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.
}
项目页面:nv-ios-version
TakahikoKawasaki/nv-ios-version
博客:使用Version类在运行时获取和比较iOS版本
使用Version类在运行时获取和比较iOS版本
if #available(iOS 9, *) {}
。从 Xcode 9 开始,在 Objective-C 中使用if (@available(iOS 11, *)) {}
。 - Cœur