6

In my AppDelegate I am using the appearance proxy to make custom UI:

//Setup custom appearances
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"header"] forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setTintColor:[UIColor blackColor]];
    [[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:@"header"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

This will crash in iOS4. How can I check if this feature is available on the iOS version they are running, so I can avoid the crash?

chown
  • 51,908
  • 16
  • 134
  • 170
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
  • 1
    possible duplicate of [Check iPhone iOS Version](http://stackoverflow.com/questions/3339722/check-iphone-ios-version) – jscs Nov 05 '11 at 02:27

2 Answers2

19

Don't check the OS, check if the capability exists.

if ([[UINavigationBar class] respondsToSelector:@selector(appearance)]) {}

Joshua Weinberg
  • 28,598
  • 2
  • 97
  • 90
  • 2
    Shocking. I don't see any harm in checking OS version. The mere presence of a symbol does not make its underlying implementation equal between OS releases. – bioffe Nov 04 '11 at 20:20
  • 5
    Apple does not recommend checking the version. You should look for features you are interested in, as @joshua stated. – logancautrell Nov 04 '11 at 20:23
  • 1
    While I agree fundamentally (except in rare cases), I believe that recommending a different method is not a complete answer. An exception case for me was a workaround for a frameworks bug in iOS 4 that caused issues in iOS 5, where the APIs were unchanged. A complete answer answers the question, then recommends the better method. – Jacob Jennings Nov 04 '11 at 21:47
  • Working around OS bugs is a whole different issue. – Joshua Weinberg Nov 04 '11 at 22:39
  • Actually ```- respondsToSelector``` is an instance method. For classes you should use ```class_respondsToSelector(Class c, SEL s);``` – Javier Soto May 15 '12 at 03:44
  • 1
    classes are objects, calling an instance method on the class like I did is perfectly valid, and will give the results expected. – Joshua Weinberg May 15 '12 at 07:17
  • instead of `[[UINavigationBar class] respondsToSelector:@selector(appearance)]` you can just do `[UINavigationBar respondsToSelector:@selector(appearance)]` – user102008 Jun 12 '12 at 21:52
1

Try this:

[UIDevice currentDevice].systemVersion
borisdiakur
  • 10,387
  • 7
  • 68
  • 100