如何检查iOS版本?
  我想检查设备的iOS版本是否大于3.1.3我尝试了如下方法: 
[[UIDevice currentDevice].systemVersion floatValue]
但它不起作用,我只想要一个:
if (version > 3.1.3) { }
我怎样才能做到这一点?
快速回答...
  从Swift 2.0开始,您可以在if或guard使用#available来保护只应在特定系统上运行的代码。 
 if #available(iOS 9, *) {} 
 
  在Objective-C中,您需要检查系统版本并进行比较。 
  iOS 8及更高版本中的[[NSProcessInfo processInfo] operatingSystemVersion] 。 
从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
  项目针对老年系统可以使用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;
}
  如果由于某种原因你认为systemVersion是你想要的,请确保将它视为一个字符串,否则你可能会截断补丁版本号(例如3.1.2  - > 3.1)。 
/*
 *  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")) {
    ...
}
正如苹果官方文档提示:您可以使用NSFoundationVersionNumber ,从NSObjCRuntime.h头文件。 
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
    // here you go with iOS 7
}
上一篇: How to check iOS version?
下一篇: Firefox addon won't load jquery as content script on when running Windows
