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? There is a certain bug in 3.0 but not i开发者_运维知识库n 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.
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. To get the OS version, use [UIDevice currentDevice]. systemVersion. To get the SDK version, I think you can use __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".
精彩评论