2

How can I execute code only for iOS 5 with iOS<5 compatibility? I have written this code:

 BOOL isIOS5 = [[[UIDevice currentDevice] systemVersion] floatValue] > 4.3;
 if (isIOS5) {

    [[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"cabecera.png"] forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance]setTintColor:[UIColor colorWithRed:80.0f/255.0f green:150.0f/255.0f blue:185.0f/255.0f alpha:1]];

 }

If I execute the app in iOS 5 it works fine, but if I try to execute the app in iOS <5 emulator it breaks. Is there a way to write an app who has code only for iOS5 but ignores it when iOS<5?

Michael J. Barber
  • 24,518
  • 9
  • 68
  • 88
Jimmy
  • 873
  • 3
  • 12
  • 29
  • This previous answer should help you out: [How to target a specific iPhone version?][1] [1]: http://stackoverflow.com/questions/820142/how-to-target-a-specific-iphone-version – sosborn Oct 27 '11 at 07:57
  • http://stackoverflow.com/questions/820142/how-to-target-a-specific-iphone-version – Kheldar Oct 27 '11 at 07:58
  • Supposing Apple release iOS 4.4 with security fixes for older devices? – johnsyweb Oct 27 '11 at 09:58

4 Answers4

9

In your case, you should check if the method is available in the current iOS Version:

if([UINavigationBar respondsToSelector:@selector(appearance)]) //iOS >=5.0
{
    [[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"cabecera.png"] forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance]setTintColor:[UIColor colorWithRed:80.0f/255.0f green:150.0f/255.0f blue:185.0f/255.0f alpha:1]];
}

Please also see this question/answer.

Community
  • 1
  • 1
Daniel
  • 20,420
  • 10
  • 92
  • 149
  • Thanks, but that code breaks: 2011-10-27 10:09:11.236 IOSBoilerplate[8045:e903] +[UINavigationBar appearance]: unrecognized selector sent to class 0xed7544 2011-10-27 10:09:11.239 IOSBoilerplate[8045:e903] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[UINavigationBar appearance]: unrecognized selector sent to class 0xed7544' – Jimmy Oct 27 '11 at 08:14
  • `[[UINavigationBar class] respondsToSelector:@selector(appearance)]` can be written as `[UINavigationBar respondsToSelector:@selector(appearance)]` – user102008 Jun 12 '12 at 21:59
6

You shouldn't test for the version of the OS.

Instead you should test if the feature is available or not.

Especially test if UINavigationBar responds to the @selector(appearance) selector.

You should read this Apple documentation which explains it all (especially this page)

AliSoftware
  • 32,623
  • 6
  • 82
  • 77
1

The answers here aren't entirely correct. If we were talking about a backwards compatible API then they'd be correct, you wouldn't need to check the version number at runtime. But in the case of iOS, the reality is that there are situations when you need to because Apple haven't just added functionality as you might expect, they occasionally alter existing functionality in incompatible ways when a new release is made.

A prime example being UIViewController's parentViewController:

Prior to iOS 5.0, if a view did not have a parent view controller and was being presented, the presenting view controller would be returned. On iOS 5, this behavior no longer occurs.

In which case the way you checked the version at runtime is fine.

Benjamin Dobell
  • 4,042
  • 1
  • 32
  • 44
-1

I am not very much sure about this but you can this also..

Click on your **project**->go to **info**-> go to **build** section-> in that go to **Deployment** section->then **IOS deployment** section choose the **deployment target** you want and save and run...

Hope it may help you... :)

mAc
  • 2,434
  • 2
  • 22
  • 39