Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obj-C, conditionally run code only if iOS5 is available?

How can I check and conditionally only compile / run code if iOS5 is available ?

like image 496
Jules Avatar asked Jan 17 '23 02:01

Jules


2 Answers

You can either check the systemVersion property of UIDevice like so:

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0f) {
    // Do something
}

But personally I don't like that method as I don't like the parsing of the string returned from systemVersion and the comparison done like that.

The best way is to check that whatever class / method it is that you want to use, exists. For example:

If you want to use TWRequest from the Twitter framework:

Class twRequestClass = NSClassFromString(@"TWRequest");
if (twRequestClass != nil) {
    // The class exists, so we can use it
} else {
    // The class doesn't exist
}

Or if you want to use startMonitoringForRegion: from CLLocationManager which was brought in in iOS 5.0:

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
...
if ([locationManager respondsToSelector:@selector(startMonitoringForRegion:)]) {
    // Yep, it responds
} else {
    // Nope, doesn't respond
}

In general it's better to do checks like that than to look at the system version.

like image 54
mattjgalloway Avatar answered Jan 22 '23 02:01

mattjgalloway


Try out this code:

if([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0)
{
     //Do stuff for iOS 5.0
}

Hope this helps you.

like image 27
jacekmigacz Avatar answered Jan 22 '23 01:01

jacekmigacz