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

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

+4
source share
2 answers

You can check the systemVersion UIDevice property as follows:

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

But personally, I don’t like this method, because I don’t like parsing the string returned from systemVersion and comparing it like this.

The best way is to verify that any class / method you want to use exists. For instance:

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 the CLLocationManager that was included 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 is better to do such checks than to look at the version of the system.

+5
source

Try this code:

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

Hope this helps you.

+4
source

Source: https://habr.com/ru/post/1403609/


All Articles