我们如何在程序中检测iOS设备运行的版本?

297

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

我该如何以编程方式检测用户设备上运行的 iOS 版本?

谢谢!


这个链接可能会有所帮助:http://jayprakashdubey.blogspot.in/2014/07/check-device-os-version.html - Jayprakash Dubey
10个回答

692

最佳当前版本,无需在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 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(浮点数或整数)。如果要精确比较它们,您需要将其分离为 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")
  }

29
注意,浮点数5.0.1的值是5。 - Michael
1
@SpencerWilliams 因为它不能很好地处理较小的版本号,所以如果你只需要识别5和6,那么它就足够了,但无法识别5.0.1和5.1.0之间的区别。 - Marek Sebera
你好,这个程序已经测试过能在iOS 7上运行吗?谢谢。 - brain56
3
谢谢。我也有机会尝试了一下,可以确认这个解决方案适用于iOS 7。 - brain56
1
注意 - 这是正确的,但相对较慢。我刚刚在Instruments的最重的跟踪底部找到了一些基于这个答案的代码,它是从scrollViewDidScroll:中调用的 - 显然可以用不同的方式编写,但它还没有被改写。 - Adam Eberbach
显示剩余7条评论

262

更新

从iOS 8开始,我们可以在NSProcessInfo上使用新的isOperatingSystemAtLeastVersion方法。

   NSOperatingSystemVersion ios8_0_1 = (NSOperatingSystemVersion){8, 0, 1};
   if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:ios8_0_1]) {
      // iOS 8.0.1 and above logic
   } else {
      // iOS 8.0.0 and below logic
   }

请注意,这个API在iOS 7上并不存在,所以会导致崩溃。如果你要支持iOS 7及以下版本,可以使用以下代码进行检查:

if ([NSProcessInfo instancesRespondToSelector:@selector(isOperatingSystemAtLeastVersion:)]) {
  // conditionally check for any version >= iOS 8 using 'isOperatingSystemAtLeastVersion'
} else {
  // we're on iOS 7 or below
}

iOS < 8 的原始答案

为了完整起见,这里提供了一种由苹果公司在 iOS 7 UI 过渡指南 中提出的替代方法,该方法涉及检查基础框架版本。

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
   // Load resources for iOS 6.1 or earlier
} else {
   // Load resources for iOS 7 or later
}

4
目前这是最佳答案,应该置于最上方。 - Josh Brown
1
有点困扰的是可能会有6.2版本发布,这样很多代码都会出问题。但是,毕竟是由苹果推荐的... - Niklas Berglund
1
注意,根据此评论:https://dev59.com/zXnZa4cB1Zd3GeqPmih0#_53knYgBc1ULPQZFfo9L,6.1和6.0的常量具有相同的值。 - Niklas Berglund
3
显然,苹果尚未为7或7.1版本包含NSFoundationVersionNumbers。因此,我想不要使用这种方法。 - Bob Spryn
3
请注意,在iOS 7及以下版本中调用 isOperatingSystemAtLeastVersion 将会触发一个未识别的实例异常选择器。 - edc1591
显示剩余2条评论

23

我知道我回答这个问题已经太晚了。我不确定我的方法是否仍适用于低版本的iOS(<5.0):

NSString *platform = [UIDevice currentDevice].model;

NSLog(@"[UIDevice currentDevice].model: %@",platform);
NSLog(@"[UIDevice currentDevice].description: %@",[UIDevice currentDevice].description);
NSLog(@"[UIDevice currentDevice].localizedModel: %@",[UIDevice currentDevice].localizedModel);
NSLog(@"[UIDevice currentDevice].name: %@",[UIDevice currentDevice].name);
NSLog(@"[UIDevice currentDevice].systemVersion: %@",[UIDevice currentDevice].systemVersion);
NSLog(@"[UIDevice currentDevice].systemName: %@",[UIDevice currentDevice].systemName);

您可以获得以下结果:

[UIDevice currentDevice].model: iPhone
[UIDevice currentDevice].description: <UIDevice: 0x1cd75c70>
[UIDevice currentDevice].localizedModel: iPhone
[UIDevice currentDevice].name: Someones-iPhone002
[UIDevice currentDevice].systemVersion: 6.1.3
[UIDevice currentDevice].systemName: iPhone OS

15
[[UIDevice currentDevice] systemVersion]

14

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

}

希望这可以帮助到您


7
你使用的宏定义在这个Stack Overflow回答中:https://dev59.com/oFzUa4cB1Zd3GeqP7-sE#8042056。 - Dan J

9
[[[UIDevice currentDevice] systemVersion] floatValue]

最简单的方法。在Swift中使用(UIDevice.currentDevice().systemVersion as NSString).floatValue - Stan
它在我的情况下也起作用了。非常简单易用。 - Anil Gupta
//大于10.0f---- if([[[UIDevice currentDevice] systemVersion] floatValue] > 10.0f){ } - Anil Gupta
不适用于像6.0.7这样的操作系统(其floatValue将为7),版本字符串中的双小数点可能会破坏对版本字符串的'floatValue'解析。 - Motti Shneor

5

Marek Sebera的工具大多数时候都很好用,但如果你像我一样需要经常检查iOS版本,你就不想在内存中频繁运行宏,因为这会导致非常轻微的减速,特别是在旧设备上。

相反,你希望将iOS版本计算为一个浮点数并将其存储在某个地方。在我的情况下,我有一个GlobalVariables单例类,我使用这个类来检查我的代码中的iOS版本,例如:

if ([GlobalVariables sharedVariables].iOSVersion >= 6.0f) {
    // do something if iOS is 6.0 or greater
}

为了在你的应用中启用这个功能,请使用以下代码(适用于使用ARC的iOS 5+):
GlobalVariables.h:
@interface GlobalVariables : NSObject

@property (nonatomic) CGFloat iOSVersion;

    + (GlobalVariables *)sharedVariables;

@end

GlobalVariables.m:

@implementation GlobalVariables

@synthesize iOSVersion;

+ (GlobalVariables *)sharedVariables {
    // set up the global variables as a static object
    static GlobalVariables *globalVariables = nil;
    // check if global variables exist
    if (globalVariables == nil) {
        // if no, create the global variables class
        globalVariables = [[GlobalVariables alloc] init];
        // get system version
        NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
        // separate system version by periods
        NSArray *systemVersionComponents = [systemVersion componentsSeparatedByString:@"."];
        // set ios version
        globalVariables.iOSVersion = [[NSString stringWithFormat:@"%01d.%02d%02d", \
                                       systemVersionComponents.count < 1 ? 0 : \
                                       [[systemVersionComponents objectAtIndex:0] integerValue], \
                                       systemVersionComponents.count < 2 ? 0 : \
                                       [[systemVersionComponents objectAtIndex:1] integerValue], \
                                       systemVersionComponents.count < 3 ? 0 : \
                                       [[systemVersionComponents objectAtIndex:2] integerValue] \
                                       ] floatValue];
    }
    // return singleton instance
    return globalVariables;
}

@end

现在,您可以轻松地检查iOS版本而不必不断运行宏。请注意,我将[[UIDevice currentDevice] systemVersion] NSString转换为CGFloat,这样就可以随时访问,而不使用本页上已经指出的任何不当方法。我的方法假定版本字符串采用n.nn.nn格式(允许后面的位数缺失),适用于iOS5+。在测试中,此方法比不断运行宏要快得多。
希望这能帮助到遇到与我相同问题的人!

4
在MonoTouch中: 要获取主版本,请使用:
UIDevice.CurrentDevice.SystemVersion.Split('.')[0]

对于小版本,请使用:

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

2
为了获取更具体的版本号信息,并将主要版本和次要版本分开:
```html

要获取更具体的版本号信息并将主要版本和次要版本分开:

```
NSString* versionString = [UIDevice currentDevice].systemVersion;
NSArray* vN = [versionString componentsSeparatedByString:@"."];

数组vN将包含主版本和次版本作为字符串,但如果您想进行比较,则应该将版本号存储为数字(整数)。您可以添加以下代码将它们存储在C数组* versionNumbers中:

int versionNumbers[vN.count];
for (int i = 0; i < sizeof(versionNumbers)/sizeof(versionNumbers[0]); i++)
    versionNumbers[i] = [[vN objectAtIndex:i] integerValue];

* 这里使用C数组是为了更简洁的语法。


0
一个简单的检查iOS版本是否小于5(所有版本)的方法:
if([[[UIDevice currentDevice] systemVersion] integerValue] < 5){
        // do something
};

3
系统版本不是整数值。 - Johan Karlsson
这个太糟糕了,试试这个:http://www.techpaa.com/2012/11/best-way-checking-iphone-ios-version.html - ShivaPrasad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接