Showing posts with label float. Show all posts
Showing posts with label float. Show all posts

Monday, December 20, 2010

How to Detect iOS Version in Code

Bumbed into a case where code crashed iOS 4.0.2, but worked with iOS 4.2. Since crash happened because of missing NSString *const, couldn't use respondsToSelector to choose, which code to run. Decided to simply check what is current iOS version:
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version > 4.0)
{
    // iOS 4.2 specific code
}
else
{
    // iOS 4.0.2 specific code
}
Please note how three part version number gets converted to float:
NSString *s = [[UIDevice currentDevice] systemVersion];
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
NSLog(@"Version: %@ and %f", s, version);
NSLog(@"Version: %@ and %f", @"3.1.3", [@"3.1.3" floatValue]);

Version: 4.0.2 and 4.000000
Version: 3.1.3 and 3.100000
I'm not really happy with the solution, but it works. Can live with that.

Update:

Always run code in real device! My development device iPhone4 with iOS 4.1 returns "4.1" as string, but that gives 4.0999999 as float value !!!