我们如何以编程方式检测设备正在运行哪个 iOS 版本? [复制]

2024-04-25

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

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

Thanks!


当前最佳版本,不需要在 NSString 中处理数字搜索就是定义macros(见原答案:检查 iPhone iOS 版本 https://stackoverflow.com/a/5337804/492624)

这些宏确实存在于 github 中,请参阅:https://github.com/carlj/CJAMacros/blob/master/CJAMacros/CJAMacros.h 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]

像这样

两个值(floatValue、intValue)都将因其类型而被剥离,5.0.1 将变为 5.0 或 5(float 或 int),为了精确比较,您必须将其分隔为 INT 数组 在这里检查接受的答案:检查 iPhone iOS 版本 https://stackoverflow.com/a/3339787/492624

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")
  }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我们如何以编程方式检测设备正在运行哪个 iOS 版本? [复制] 的相关文章

随机推荐