float version = [[[UIDevice currentDevice] systemVersion] floatValue];Please note how three part version number gets converted to float:
if (version > 4.0)
{
// iOS 4.2 specific code
}
else
{
// iOS 4.0.2 specific code
}
NSString *s = [[UIDevice currentDevice] systemVersion];I'm not really happy with the solution, but it works. Can live with that.
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
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 !!!
I've written a post about how to properly detect iOS version. You can take a look here. http://formtools.tumblr.com/post/2080375589/detecting-ios-version
ReplyDeleteAlso, useful is if you need the version data for pre-processor macros.
ReplyDelete#define __IPHONE_2_0 20000
...
#define __IPHONE_4_2 40200
For legacy support for things like...
#ifdef __IPHONE_4_0
@interface YOURCLASS : NSObject
#else
@interface YOURCLASS : NSObject
#endif
Text comparision can be useful, true. Depends how reliable comparision is, had some problems with it http://stackoverflow.com/questions/3861447/problem-using-nsstring-compareoptionsrange-conflicting-results
ReplyDeletePre-processor macros are useful, when you need to decide at application build time which code to take. Not so helpful, when you need to run same app (binary) in several incompatible iOS versions.
Thanx for comments!