43

I need to reconfigure some UI based on the iOS version I am running against, so I need a good way of checking the iOS version. For the time being I am doing this:

if ([[[UIDevice currentDevice] systemVersion] isEqualToString: @"7.0"]) {
    //do stuff
}

I'd prefer not to hard code these string comparisons and make decisions based on that. Are there any better ways of doing this?

alexgophermix
  • 4,189
  • 5
  • 32
  • 59
Huang
  • 1,355
  • 2
  • 11
  • 28
  • That will break if iOS 7.1 comes out. But you're on the right track.Split the string up and compare the major version only, if you care about iOS7 in general. – Krumelur Sep 24 '13 at 21:59
  • 1
    Have you looked at [this answer?][1] [1]: http://stackoverflow.com/questions/12561599/how-to-check-ios-version-is-ios-6/12561661#12561661 – shrimp Sep 24 '13 at 22:05
  • this looks the most stable – Huang Sep 24 '13 at 22:13

2 Answers2

126

I always keep those in my Constants.h file:

#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES) 
#define IS_OS_5_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0)
#define IS_OS_6_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
#define IS_OS_7_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
#define IS_OS_8_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
#define IS_OS_9_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0)

Although I'll always prefer the above macros, for the completion of the accepted answer, here is the apple approved way:

if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {

    // do stuff for iOS 7 and newer

}
else {

    // do stuff for older versions than iOS 7
}
Parth Bhadaja
  • 416
  • 3
  • 15
Segev
  • 19,035
  • 12
  • 80
  • 152
  • 3
    cool man, nice utility belt. – Huang Sep 24 '13 at 22:13
  • 1
    side effect of this is [[[UIDevice currentDevice] systemVersion] floatValue] will be called each time you use the macro. The systemVersion method has historically been surprisingly slow, so if you have recursion of any description cache the result value. – Nick Hingston Nov 21 '13 at 11:11
  • 4
    This is really not a good approach, for a number of reasons. For specifics, please see the comments on [this post](http://stackoverflow.com/a/3341214/214149). – Mac Nov 27 '13 at 04:44
  • 2
    Why i prefer my answer: 1. I only care about big releases 5,6,7 so float is just fine. 2. So far this raw floating point comparison never failed me. Sometimes convenience will win in battle against "you must be aware of that edge case that will never happen". – Segev Dec 30 '13 at 06:33
  • Nice collection! Added :) – Hjalmar Jan 23 '14 at 13:23
  • If you want to differentiate between iOS7 and iOS8, keep in mind that you should check firstly for iOS8 and later, then for iOS7...iOS6... and so on – jomafer Sep 11 '14 at 14:32
  • I don't like the first approach because you conflate macros and conditional compilation with a runtime check for the system version, confusing to me, I would have just made a static method or something like that – marchinram Apr 05 '16 at 16:10
  • Do not use these macros. There are proper ways to check APIs at runtime. – rmaddy Apr 28 '16 at 17:36
117
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {

    // do stuff for iOS 7 and newer

}
else {

    // do stuff for older versions than iOS 7
}
Ar Ma
  • 1,332
  • 1
  • 7
  • 5
  • 3
    +1 I have never seen this before. – Brian Nickel Sep 24 '13 at 23:02
  • 13
    For what it's worth, this was Apple's suggestion at the 2013 Tech Talk in the adopting iOS 7 session - although they wrapped `NSFoundationVersionNumber` in a call to `floor()`. – Charles A. Oct 15 '13 at 23:11
  • Wouldn't there be a 6.2.x which would make the check invalid? – xu huanze Nov 19 '13 at 14:30
  • 4
    Nope--that wouldn't invalidate the check. Have a look at NSObjCRuntime.h. – GarlicFries Jan 02 '14 at 15:25
  • 2
    It's also in print in the iOS 7 Transition Guide: [link](https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TransitionGuide/SupportingEarlieriOS.html). See "Loading Resources Conditionally." – Geoff Hom Apr 01 '14 at 23:30
  • 2
    Currently the last version to check is still iOS7: #define NSFoundationVersionNumber_iOS_7_1 1047.25 – jomafer Sep 11 '14 at 14:35