Jules
Jules

Reputation: 7766

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

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

Upvotes: 4

Views: 867

Answers (2)

mattjgalloway
mattjgalloway

Reputation: 34912

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.

Upvotes: 5

jacekmigacz
jacekmigacz

Reputation: 789

Try out this code:

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

Hope this helps you.

Upvotes: 4

Related Questions