Check OS version in Swift?
For iOS, try:
var systemVersion = UIDevice.current.systemVersion
For OS X, try:
var systemVersion = NSProcessInfo.processInfo().operatingSystemVersion
If you just want to check if the users is running at least a specific version, you can also use the following Swift 2 feature which works on iOS and OS X:
if #available(iOS 9.0, *) {
// use the feature only available in iOS 9
// for ex. UIStackView
} else {
// or use some work around
}
BUT it is not recommended to check the OS version. It is better to check if the feature you want to use is available on the device than comparing version numbers.
For iOS, as mentioned above, you should check if it responds to a selector;
eg.:
if (self.respondsToSelector(Selector("showViewController"))) {
self.showViewController(vc, sender: self)
} else {
// some work around
}
Swift MacOS know the version of the OS and Xcode
For the macOS version, you can indeed get it from ProcessInfo.processInfo.operatingSystemVersion
.
For the Xcode version, you can first find where Xcode's bundle is from its bundle ID, find its bundle, and get the version from its bundle's Info.plist as a string.
guard let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: "com.apple.dt.Xcode"),
let bundle = Bundle(url: url) else {
print("Xcode is not installed")
exit(1)
}
guard let infoDict = bundle.infoDictionary,
let version = infoDict["CFBundleShortVersionString"] as? String else {
print("No version found in Info.plist")
exit(1)
}
print(version) // Example output: 13.1
You can replace the let url = ...
step with a NSOpenPanel
prompt to let the user choose where their Xcode is installed too.
How to check iOS version?
The quick answer …
As of Swift 2.0, you can use #available
in an if
or guard
to protect code that should only be run on certain systems.
if #available(iOS 9, *) {}
In Objective-C, you need to check the system version and perform a comparison.
[[NSProcessInfo processInfo] operatingSystemVersion]
in iOS 8 and above.
As of Xcode 9:
if (@available(iOS 9, *)) {}
The full answer …
In Objective-C, and Swift in rare cases, it's better to avoid relying on the operating system version as an indication of device or OS capabilities. There is usually a more reliable method of checking whether a particular feature or class is available.
Checking for the presence of APIs:
For example, you can check if UIPopoverController
is available on the current device using NSClassFromString
:
if (NSClassFromString(@"UIPopoverController")) {
// Do something
}
For weakly linked classes, it is safe to message the class, directly. Notably, this works for frameworks that aren't explicitly linked as "Required". For missing classes, the expression evaluates to nil, failing the condition:
if ([LAContext class]) {
// Do something
}
Some classes, like CLLocationManager
and UIDevice
, provide methods to check device capabilities:
if ([CLLocationManager headingAvailable]) {
// Do something
}
Checking for the presence of symbols:
Very occasionally, you must check for the presence of a constant. This came up in iOS 8 with the introduction of UIApplicationOpenSettingsURLString
, used to load Settings app via -openURL:
. The value didn't exist prior to iOS 8. Passing nil to this API will crash, so you must take care to verify the existence of the constant first:
if (&UIApplicationOpenSettingsURLString != NULL) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
Comparing against the operating system version:
Let's assume you're faced with the relatively rare need to check the operating system version. For projects targeting iOS 8 and above, NSProcessInfo
includes a method for performing version comparisons with less chance of error:
- (BOOL)isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion)version
Projects targeting older systems can use systemVersion
on UIDevice
. Apple uses it in their GLSprite sample code.
// A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
// class is used as fallback when it isn't available.
NSString *reqSysVer = @"3.1";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending) {
displayLinkSupported = TRUE;
}
If for whatever reason you decide that systemVersion
is what you want, make sure to treat it as an string or you risk truncating the patch revision number (eg. 3.1.2 -> 3.1).
How do I determine the OS version at runtime in OS X or iOS (without using Gestalt)?
On OS X 10.10 (and iOS 8.0), you can use [[NSProcessInfo processInfo] operatingSystemVersion]
which returns a NSOperatingSystemVersion
struct, defined as
typedef struct {
NSInteger majorVersion;
NSInteger minorVersion;
NSInteger patchVersion;
} NSOperatingSystemVersion;
There is also a method in NSProcessInfo
that will do the comparison for you:
- (BOOL)isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion)version
Beware, although documented to be available in OS X 10.10 and later, both operatingSystemVersion
and isOperatingSystemAtLeastVersion:
exist on OS X 10.9 (probably 10.9.2) and work as expected. It means that you must not test if NSProcessInfo
responds to these selectors to check if you are running on OS X 10.9 or 10.10.
On iOS, these methods are effectively only available since iOS 8.0.
How can I determine the latest iOS version programmatically using Swift?
Apple provides an API at curl http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStore.woa/wa/com.apple.jingle.appserver.client.MZITunesClientCheck/version
. Test it on the command line using:
curl http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStore.woa/wa/com.apple.jingle.appserver.client.MZITunesClientCheck/version
The response is XML, and the information you need is keyed by device version (eg iPhone8,1
).
You can access your device's version by using sysctlbyname()
from Swift.
Update Looks like the old URL (http://phobos.apple.com./version
) died a while back (thanks Troy) so have updated my answer with another version. I have no idea if the content is the same.
Related Topics
Missing Return Uitableviewcell
Unwind Segue Not Working in iOS 8
Adding Custom Fonts to iOS App Finding Their Real Names
What's the Best Way to Detect When the App Is Entering the Background For My View
Uibutton: How to Center an Image and a Text Using Imageedgeinsets and Titleedgeinsets
How to Make Drawrect Work Right Now
Uibutton Inside a View That Has a Uitapgesturerecognizer
Drawing Uibezierpath on Code Generated Uiview
iOS 7 Tableview Like in Settings App on iPad
Uitableview with Fixed Section Headers
How Add Separator to String at Every N Characters in Swift