How can I check and conditionally only compile / run code if iOS5 is available ?
Asked
Active
Viewed 860 times
4
-
4You don't want to conditionally compile the code otherwise that will only compile the code based on what you have on your build machine and then the code won't be available if you run it on an iOS5 device. – Nick Bull Mar 27 '12 at 08:20
-
have a look at [Check iPhone iOS Version](http://stackoverflow.com/questions/3339722/check-iphone-ios-version) – Matthias Bauch Mar 27 '12 at 08:25
2 Answers
5
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.

mattjgalloway
- 34,792
- 12
- 100
- 110
4
Try out this code:
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0)
{
//Do stuff for iOS 5.0
}
Hope this helps you.

Parth Bhatt
- 19,381
- 28
- 133
- 216

jacekmigacz
- 789
- 12
- 22