views:

1722

answers:

5

Hi, I want to check if the iOS version of the device is greater then the 3.1.3 I tried things like [[UIDevice currentDevice].systemVersion floatValue]

but does not work, I just want a if (version > 3.1.3) { }

Does anybody know how to do this?

Thanks

+2  A: 

Try:

NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: @"3.1.3" options: NSNumericSearch];
if (order == NSOrderedSame || order == NSOrderedDescending) {
    // OS version >= 3.1.3
} else {
    // OS version < 3.1.3
}
Jonathan Grynspan
+3  A: 

You can get the OS version using:

[[UIDevice currentDevice] systemVersion]

However, you should avoid relying on the version string as an indication of device or OS capabilities. There is usually a more reliable method of checking whether a particular feature or class is available. For example, you can check if UIPopoverController is available on the current device using NSClassFromString:

if(NSClassFromString(@"UIPopoverController")) {
    // Do something
}

Some classes, like CLLocationManager and UIDevice, provide methods to check device capabilities:

if([CLLocationManager headingAvailable]) {
    // Do something
}

Apple uses systemVersion in their GLSprite sample code, so my recommendation can't be absolute:

// A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
// class is used as fallback when it isn't available.
NSString *reqSysVer = @"3.1";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
    displayLinkSupported = TRUE;

Important Note: If for whatever reason you decide that systemVersion is what you want, make sure to treat it as an string or you risk truncating the minor revision number (eg. 3.1.2 -> 3.1).

Justin
There are specific cases where checking for system version is warranted. For example, a couple of classes and methods that were private n 3.x were made public in 4.0, so if you simply checked for their availability you would get a wrong result in 3.x. Additionally, the way that UIScrollViews handle zoom scales changed subtly in 3.2 and above, so you would need to check OS versions in order to process the results appropriately.
Brad Larson
Good clarification, thank you.
Justin
+1  A: 

Try this blog: http://cocoawithlove.com/2010/07/tips-tricks-for-conditional-ios3-ios32.html

hiepnd
Checking for supported features is a much better approach to take, I think.
Kendall Helmstetter Gelner
+1  A: 

I recommend:

if ([[[UIDevice currentDevice] systemVersion] floatValue] > 3.13) {
    ; // ...
}

credit: http://stackoverflow.com/questions/820142/how-to-target-a-specific-iphone-version

ohho
A: 

I find nice utility method in this link http://cocoabugs.blogspot.com/2010/09/utility-method-to-know-ios4-installed.html

jeeva