我想检查用户是否在iOS 5.0以下运行应用程序,并在应用程序中显示一个标签。

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

谢谢!


当前回答

[[[UIDevice currentDevice] systemVersion] floatValue]

其他回答

一个简单的检查iOS版本小于5(所有版本):

if([[[UIDevice currentDevice] systemVersion] integerValue] < 5){
        // do something
};
[[UIDevice currentDevice] systemVersion]

当前最好的版本,不需要在NSString中处理数字搜索是定义宏(查看原始答案:Check iPhone iOS版本)

这些宏确实存在于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]

像这样

这两个值(floatValue, intValue)将由于其类型而被剥离,5.0.1将变成5.0或5 (float或int),为了精确比较,你必须将它分离为int数组 检查接受的答案在这里:检查iPhone iOS版本

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")
  }

[[UIDevice currentDevice] systemVersion];

或者像这样检查版本

你可以从这里得到下面的宏。

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(IOS_VERSION_3_2_0))      
{

        UIImageView *background = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cs_lines_back.png"]] autorelease];
        theTableView.backgroundView = background;

}

希望这能有所帮助

在MonoTouch:

要获得Major版本,请使用:

UIDevice.CurrentDevice.SystemVersion.Split('.')[0]

对于小版本使用:

UIDevice.CurrentDevice.SystemVersion.Split('.')[1]