iphone - 我们如何以编程方式检测设备在哪个 iOS 版本上运行?

这个问题在这里已经有了答案:
关闭8年前

我想检查用户是否在低于 5.0 的 iOS 上运行应用并在应用中显示标签。

如何以编程方式检测用户设备上运行的是哪个 iOS?

谢谢!

最佳答案

当前最佳版本,无需处理NSString中的数字搜索就是定义macros(见原答案:Check iPhone iOS Version)

这些宏确实存在于 github 中,请参阅:https://github.com/carlj/CJAMacros/blob/master/CJAMacros/CJAMacros.h

像这样:

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

并像这样使用它们:

if (SYSTEM_VERSION_LESS_THAN(@"5.0")) {
    // code here
}

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) {
    // code here
}

以下是过时版本

获取操作系统版本:

[[UIDevice currentDevice] systemVersion]

返回字符串,可以通过转成int/float

-[NSString floatValue]
-[NSString intValue]

喜欢这个

Both values (floatValue, intValue) will be stripped due to its type, 5.0.1 will become 5.0 or 5 (float or int), for comparing precisely, you will have to separate it to array of INTs check accepted answer here: Check iPhone iOS Version

NSString *ver = [[UIDevice currentDevice] systemVersion];
int ver_int = [ver intValue];
float ver_float = [ver floatValue];

然后像这样比较

NSLog(@"System Version is %@",[[UIDevice currentDevice] systemVersion]);
NSString *ver = [[UIDevice currentDevice] systemVersion];
float ver_float = [ver floatValue];
if (ver_float < 5.0) return false;

适用于 Swift 4.0 语法

以下示例只是检查设备是否为 iOS11 或更高版本。

let systemVersion = UIDevice.current.systemVersion
if systemVersion.cgFloatValue >= 11.0 {
    //"for ios 11"
  }
else{
   //"ios below 11")
  }

https://stackoverflow.com/questions/7848766/

相关文章:

objective-c - block 声明语法列表

ios - 在 Swift 中实例化并展示一个 viewController

ios - Objective-C 中的 "@private"是什么意思?

ios - UIActivityViewController 在 iOS 8 iPad 上崩溃

objective-c - #pragma 标记的意义​​是什么?为什么我们需要#pragma 标记

ios - UIScrollView 以编程方式滚动到底部

objective-c - 将项目转换为使用 ARC 时, "switch case is in p

ios - UITableview:如何禁用某些行而不是其他行的选择

ios - 清除 NSUserDefaults

objective-c - 语义问题 : Property's synthesized getter