我们如何以编程方式检测哪个iOS版本的设备正在运行? [英] How can we programmatically detect which iOS version is device running on?

查看:97
本文介绍了我们如何以编程方式检测哪个iOS版本的设备正在运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



如何检测哪个iOS正在运行

解决方案

最佳当前版本,而不需要处理NSString中的数字搜索是定义(请参阅原始答案:检查iPhone iOS版本



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



像这样:

  #define SYSTEM_VERSION_EQUAL_TO(v)([[[UIDevice currentDevice] systemVersion] compare:v选项: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]比较:v选项:NSNumericSearch]!= NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) = NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)([[[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch]!= NSOrderedDescending)

并使用它们:

  if(SYSTEM_VERSION_LESS_THAN(@ )){
// 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 int),为了进行精确比较,您必须将其分隔为INT数组
检查接受的答案在这里:检查iPhone iOS版本




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

并像这样比较

  NSLog(@系统版本为%@,[[UIDevice currentDevice] systemVersion]); 
NSString * ver = [[UIDevice currentDevice] systemVersion];
float ver_float = [ver floatValue];
if(ver_float< 5.0)return false;


I want to check if the user is running the app on iOS less than 5.0 and display a label in the app.

How do I detect which iOS is running on user's device programmatically?

Thanks!

解决方案

Best current version, without need to deal with numeric search within NSString is to define macros (See original answer: Check iPhone iOS Version)

Those macros do exist in github, see: https://github.com/carlj/CJAMacros/blob/master/CJAMacros/CJAMacros.h

Like this:

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

and use them like this:

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

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


Outdated version below

to get OS version:

[[UIDevice currentDevice] systemVersion]

returns string, which can be turned into int/float via

-[NSString floatValue]
-[NSString intValue]

like this

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];

and compare like this

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

这篇关于我们如何以编程方式检测哪个iOS版本的设备正在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆