简体   繁体   English

如何在运行时确定当前的iPhone OS版本并比较版本字符串?

[英]How to determine the current iPhone OS version at runtime and compare version strings?

How can you determine and compare (>, <, etc.) the current OS version of the iPhone that the app is running on? 如何确定和比较(>,<等等)运行该应用程序的iPhone的当前操作系统版本? There is a certain bug in 3.0 but not in 3.1+ so I'd like to be able to skip out a bit of code if the current OS version is not >= 3.1. 3.0中有一个错误,但3.1+中没有,所以如果当前操作系统版本不是> = 3.1,我希望能够跳过一些代码。

This needs to be at runtime not compile time! 这需要在运行时而不是编译时!

You can for instance do something like this: 例如,您可以执行以下操作:

NSString *reqSysVer = @"3.1";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];

if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
 {
    //Do some clever stuff
 }

Do you mean determine the version of the OS? 你的意思是确定操作系统的版本? The SDK is fixed at build time, but the OS may change. SDK在构建时修复,但操作系统可能会更改。 To get the OS version, use [UIDevice currentDevice]. 要获取操作系统版本,请使用[UIDevice currentDevice]。 systemVersion. systemVersion。 To get the SDK version, I think you can use __IPHONE_OS_VERSION_MIN_REQUIRED. 要获得SDK版本,我认为您可以使用__IPHONE_OS_VERSION_MIN_REQUIRED。

A more reliable way is to do something like this: 更可靠的方法是做这样的事情:

static inline NSComparisonResult compareCurVersionTo(int major, int minor, int point)
{
    NSUInteger testVers = major*10000 + minor*100 + point;
    NSUInteger curSysNum = 0;
    NSUInteger multi = 10000;
    for(NSString *n in [[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."]) {
        curSysNum += [n integerValue] * multi;
        multi /= 100;
    }
    if(curSysNum < testVers) return NSOrderedAscending;
    if(curSysNum > testVers) return NSOrderedDescending;
    return NSOrderedSame;
}

This handles the "Leopard" bug where the minor releases were more than "9". 这处理“Leopard”错误,其中次要版本超过“9”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM