This question already has an answer here:
- How to check iOS version? 37 answers
I want to check if the user is running the app on iOS less than 5.0 and display a label in the app.
How do I detect which iOS is running on user's device programmatically?
Thanks!
A simple check for iOS version less than 5 (all versions):
Update
From iOS 8 we can use the new
isOperatingSystemAtLeastVersion
method onNSProcessInfo
Beware that this will crash on iOS 7, as the API didn't exist prior to iOS 8. If you're supporting iOS 7 and below, you can safely perform the check with
Original answer iOS < 8
For the sake of completeness, here's an alternative approach proposed by Apple itself in the iOS 7 UI Transition Guide, which involves checking the Foundation Framework version.
[[UIDevice currentDevice] systemVersion];
or check the version like
You can get the below Macros from here.
Hope this helps
Marek Sebera's is great most of the time, but if you're like me and find that you need to check the iOS version frequently, you don't want to constantly run a macro in memory because you'll experience a very slight slowdown, especially on older devices.
Instead, you want to compute the iOS version as a float once and store it somewhere. In my case, I have a
GlobalVariables
singleton class that I use to check the iOS version in my code using code like this:To enable this functionality in your app, use this code (for iOS 5+ using ARC):
GlobalVariables.h:
GlobalVariables.m:
Now you're able to easily check the iOS version without running macros constantly. Note in particular how I converted the
[[UIDevice currentDevice] systemVersion]
NSString to a CGFloat that is constantly accessible without using any of the improper methods many have already pointed out on this page. My approach assumes the version string is in the format n.nn.nn (allowing for later bits to be missing) and works for iOS5+. In testing, this approach runs much faster than constantly running the macro.Hope this helps anyone experiencing the issue I had!
To get more specific version number information with major and minor versions separated:
The array
vN
will contain the major and minor versions as strings, but if you want to do comparisons, version numbers should be stored as numbers (ints). You can add this code to store them in the C-array*versionNumbers
:* C-arrays used here for more concise syntax.