tags:

views:

1979

answers:

2

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 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.

This needs to be at runtime not compile time!

+7  A: 

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.

Ben Gottlieb
Ah okay, yes the current OS version! As `systemVersion` returns a string (@"1.2", @"3.1.2"), how can this be compared and perform comparisons like < or >?
Michael Waterfall
-[NSString intValue] or -[NSString floatValue]
Ben Gottlieb
Not able to test that at the moment, but how will `floatValue` handle a value such as 3.2.1? As it's not a valid floating point number?
Michael Waterfall
If you need more than just the point-release, you may need to parse the string using, for example, -[NSString componentsSeparatedByString:].
Ben Gottlieb
Thanks! I used the `systemVersion` in conjunction with a slightly modified version of this function for comparisons: http://snipplr.com/view/2771/compare-two-version-strings/
Michael Waterfall
See this snipped for a great implementation of Runtime iOS Version Checking: http://forr.st/~mOO
Michael Waterfall
+6  A: 

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
 }
Mez