Reputation: 3125
I'm trying to check system information in Swift. I figured out, that it could be achieved by code:
var sysData:CMutablePointer<utsname> = nil
let retVal:CInt = uname(sysData)
I have two problems with this code:
Upvotes: 286
Views: 245648
Reputation: 12085
For iOS, try:
var systemVersion = UIDevice.current.systemVersion
For macOS, try:
var systemVersion = ProcessInfo.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
}
Upvotes: 525
Reputation: 10839
I made this, for simple use, created an IOSVersion.swift
file and added this code :
import UIKit
enum IOSVersion {
static func SYSTEM_VERSION_EQUAL_TO(version: NSString) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version,
options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedSame
}
static func SYSTEM_VERSION_GREATER_THAN(version: NSString) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version as String,
options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedDescending
}
static func SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(version: NSString) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version as String,
options: NSStringCompareOptions.NumericSearch) != NSComparisonResult.OrderedAscending
}
static func SYSTEM_VERSION_LESS_THAN(version: NSString) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version as String,
options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedAscending
}
static func SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(version: NSString) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version as String,
options: NSStringCompareOptions.NumericSearch) != NSComparisonResult.OrderedDescending
}
}
USE :
IOSVersion.SYSTEM_VERSION_EQUAL_TO("8.0")
IOSVersion.SYSTEM_VERSION_LESS_THAN("8.0")
Thanks @KVISH
Edit Swift 2 :
if #available(iOS 9.0, *) {
// 👍
} else {
// 👎
}
Upvotes: 16
Reputation: 8311
Update:
Now you should use new availability checking introduced with Swift 2:
e.g. To check for iOS 9.0 or later use can this:
if #available(iOS 9.0, *) {
// use UIStackView
} else {
// show sad face emoji
}
or can be used with whole method or class
@available(iOS 9.0, *)
func useStackView() {
// use UIStackView
}
or with guard
guard #available(iOS 14, *) else {
return
}
For more info see this.
UPDATE: based on Allison's comment I have updated the answer, check is still runtime, but compiler can know in advance & can show better error or suggestion while you are working on it.
Other ways to check:
if you don't want exact version but want to check iOS 9,10 or 11 using if:
let floatVersion = (UIDevice.current.systemVersion as NSString).floatValue
EDIT: Just found another way to achieve this:
let iOS8 = floor(NSFoundationVersionNumber) > floor(NSFoundationVersionNumber_iOS_7_1)
let iOS7 = floor(NSFoundationVersionNumber) <= floor(NSFoundationVersionNumber_iOS_7_1)
Upvotes: 127
Reputation: 8105
We dont need to create extension since ProcessInfo
gives us the version info. You can see sample code for iOS as below.
let os = ProcessInfo().operatingSystemVersion
switch (os.majorVersion, os.minorVersion, os.patchVersion) {
case (let x, _, _) where x < 8:
print("iOS < 8.0.0")
case (8, 0, _):
print("iOS >= 8.0.0, < 8.1.0")
case (8, _, _):
print("iOS >= 8.1.0, < 9.0")
case (9, _, _):
print("iOS >= 9.0.0")
default:
print("iOS >= 10.0.0")
}
Reference: http://nshipster.com/swift-system-version-checking/
Upvotes: 70
Reputation: 52231
func run() {
let version = OperatingSystemVersion(majorVersion: 13, minorVersion: 0, patchVersion: 0)
if ProcessInfo.processInfo.isOperatingSystemAtLeast(version) {
runNewCode()
} else {
runLegacyCode()
}
}
func runNewCode() {
guard #available(iOS 13.0, *) else {
fatalError()
}
// do new stuff
}
func runLegacyCode() {
// do old stuff
}
Upvotes: 22
Reputation: 25304
extension OperatingSystemVersion {
func getFullVersion(separator: String = ".") -> String {
return "\(majorVersion)\(separator)\(minorVersion)\(separator)\(patchVersion)"
}
}
let os = ProcessInfo().operatingSystemVersion
print(os.majorVersion) // 12
print(os.minorVersion) // 2
print(os.patchVersion) // 0
print(os.getFullVersion()) // 12.2.0
Upvotes: 15
Reputation: 13208
I made helper functions that were transferred from the below link into swift:
How can we programmatically detect which iOS version is device running on?
func SYSTEM_VERSION_EQUAL_TO(version: String) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version,
options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedSame
}
func SYSTEM_VERSION_GREATER_THAN(version: String) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version,
options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedDescending
}
func SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(version: String) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version,
options: NSStringCompareOptions.NumericSearch) != NSComparisonResult.OrderedAscending
}
func SYSTEM_VERSION_LESS_THAN(version: String) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version,
options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedAscending
}
func SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(version: String) -> Bool {
return UIDevice.currentDevice().systemVersion.compare(version,
options: NSStringCompareOptions.NumericSearch) != NSComparisonResult.OrderedDescending
}
It can be used like so:
SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO("7.0")
func SYSTEM_VERSION_EQUAL_TO(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) == .orderedSame
}
func SYSTEM_VERSION_GREATER_THAN(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) == .orderedDescending
}
func SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) != .orderedAscending
}
func SYSTEM_VERSION_LESS_THAN(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) == .orderedAscending
}
func SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) != .orderedDescending
}
Upvotes: 55
Reputation: 27072
Swift 4.x
func iOS_VERSION_EQUAL_TO(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: NSString.CompareOptions.numeric) == ComparisonResult.orderedSame
}
func iOS_VERSION_GREATER_THAN(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: NSString.CompareOptions.numeric) == ComparisonResult.orderedDescending
}
func iOS_VERSION_GREATER_THAN_OR_EQUAL_TO(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: NSString.CompareOptions.numeric) != ComparisonResult.orderedAscending
}
func iOS_VERSION_LESS_THAN(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: NSString.CompareOptions.numeric) == ComparisonResult.orderedAscending
}
func iOS_VERSION_LESS_THAN_OR_EQUAL_TO(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: NSString.CompareOptions.numeric) != ComparisonResult.orderedDescending
}
Usage:
if iOS_VERSION_GREATER_THAN_OR_EQUAL_TO(version: "11.0") {
//Do something!
}
P.S. KVISH answer translated to Swift 4.x with renaming the functions as I am specifically using this snippet for the iOS app.
Upvotes: 2
Reputation: 802
Most of sample codes written here will get unexpected result with extra-zero versions. For example,
func SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) != ComparisonResult.orderedAscending
}
This method won't return true with passed version "10.3.0" in iOS "10.3". This kind of result does not make sense and they must be regarded as same version. To get accurate comparison result, we must consider comparing all of number components in version string. Plus, providing global methods in all capital letters is not a good way to go. As version type we use in our SDK is a String, it's make sense that extending comparing functionality in String.
To compare system version, all of the following examples should work.
XCTAssertTrue(UIDevice.current.systemVersion.isVersion(lessThan: "99.0.0"))
XCTAssertTrue(UIDevice.current.systemVersion.isVersion(equalTo: UIDevice.current.systemVersion))
XCTAssertTrue(UIDevice.current.systemVersion.isVersion(greaterThan: "3.5.99"))
XCTAssertTrue(UIDevice.current.systemVersion.isVersion(lessThanOrEqualTo: "10.3.0.0.0.0.0.0"))
XCTAssertTrue(UIDevice.current.systemVersion.isVersion(greaterThanOrEqualTo: "10.3"))
You can check it out in my repository here https://github.com/DragonCherry/VersionCompare
Upvotes: 0
Reputation: 51
The easiest and the simplest way to check system version (and a lot of other versions) in Swift 2 and higher is:
if #available(iOS 9.0, *) { // check for iOS 9.0 and later
}
Also, with #available
you can check versions of these:
iOS
iOSApplicationExtension
macOS
macOSApplicationExtension
watchOS
watchOSApplicationExtension
tvOS
tvOSApplicationExtension
swift
Upvotes: 5
Reputation: 123
Update for Swift 3.0+
func SYSTEM_VERSION_EQUAL_TO(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) == ComparisonResult.orderedSame
}
func SYSTEM_VERSION_GREATER_THAN(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) == ComparisonResult.orderedDescending
}
func SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) != ComparisonResult.orderedAscending
}
func SYSTEM_VERSION_LESS_THAN(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) == ComparisonResult.orderedAscending
}
func SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(version: String) -> Bool {
return UIDevice.current.systemVersion.compare(version, options: .numeric) != ComparisonResult.orderedDescending
}
Upvotes: 7
Reputation: 1496
Get current version of system and split it. So you can get major and minor version.
let sys_version = UIDevice.current.systemVersion
let all_version = sys_version.components(separatedBy: ".")
print("Major version : \(all_version[0])")
print("Minor version : \(all_version[1])")
Upvotes: 0
Reputation: 3607
let osVersion = NSProcessInfo.processInfo().operatingSystemVersion
let versionString = osVersion.majorVersion.description + "." + osVersion.minorVersion.description + "." + osVersion.patchVersion.description
print(versionString)
Upvotes: 1
Reputation: 11987
Also if you want to check WatchOS.
Swift
let watchOSVersion = WKInterfaceDevice.currentDevice().systemVersion
print("WatchOS version: \(watchOSVersion)")
Objective-C
NSString *watchOSVersion = [[WKInterfaceDevice currentDevice] systemVersion];
NSLog(@"WatchOS version: %@", watchOSVersion);
Upvotes: 0
Reputation: 5066
Based on Matt Thompson's answer, here is a method with respective unit tests that works with Swift and Objective-c on iOS 7 and above (including iOS 9 which no longer let's you check the NSFoundationNumber):
+ (BOOL) isAtLeastOSVersion:(NSString *)osVersion
{
switch ([[UIDevice currentDevice].systemVersion compare:osVersion options:NSNumericSearch]) {
case NSOrderedSame:
case NSOrderedDescending:
return YES;
default:
return NO;
}
}
.
@interface ANFakeCurrDevice : NSObject
@property (nonatomic, strong) NSString *systemVersion;
@end
@implementation ANFakeCurrDevice
@end
@implementation MyHelperClassUnitTests
- (void)setUp {
[super setUp];
}
- (void)tearDown {
[super tearDown];
}
- (void)test_isAtLeastOSVersion
{
id deviceMock = [OCMockObject niceMockForClass:[UIDevice class]];
ANFakeCurrDevice *fakeCurrDevice = [ANFakeCurrDevice new];
fakeCurrDevice.systemVersion = @"99.9.9";
[[[deviceMock stub] andReturn:fakeCurrDevice] currentDevice];
XCTAssertTrue([[UIDevice currentDevice].systemVersion isEqualToString:@"99.9.9"]);
fakeCurrDevice.systemVersion = @"1.0.1";
XCTAssertTrue([ANConstants isAtLeastOSVersion:@"1"]);
XCTAssertTrue([ANConstants isAtLeastOSVersion:@"1.0"]);
XCTAssertTrue([ANConstants isAtLeastOSVersion:@"1.0.1"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"1.0.2"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"1.1.0"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"2"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"2.0"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"2.0.0"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"2.0.1"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"2.1.0"]);
fakeCurrDevice.systemVersion = @"8.4.0";
XCTAssertTrue([ANConstants isAtLeastOSVersion:@"7.0.1"]);
XCTAssertTrue([ANConstants isAtLeastOSVersion:@"8"]);
XCTAssertTrue([ANConstants isAtLeastOSVersion:@"8.4"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"8.4.1"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"8.4.2"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"9.0"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"9.0.1"]);
XCTAssertFalse([ANConstants isAtLeastOSVersion:@"9.0.2"]);
XCTAssertTrue([ANConstants isAtLeastOSVersion:@"8.4"] && ![ANConstants isAtLeastOSVersion:@"9.0"]);
fakeCurrDevice.systemVersion = @"8.4.1";
XCTAssertTrue([ANConstants isAtLeastOSVersion:@"8.4"] && ![ANConstants isAtLeastOSVersion:@"9.0"]);
}
@end
Upvotes: 1
Reputation: 7833
If you're using Swift 2 and you want to check the OS version to use a certain API, you can use new availability feature:
if #available(iOS 8, *) {
//iOS 8+ code here.
}
else {
//Code for iOS 7 and older versions.
//An important note: if you use #availability, Xcode will also
//check that you don't use anything that was introduced in iOS 8+
//inside this `else` block. So, if you try to use UIAlertController
//here, for instance, it won't compile. And it's great.
}
I wrote this answer because it is the first question in Google for the swift 2 check system version
query.
Upvotes: 9
Reputation: 1220
let Device = UIDevice.currentDevice()
let iosVersion = NSString(string: Device.systemVersion).doubleValue
let iOS8 = iosVersion >= 8
let iOS7 = iosVersion >= 7 && iosVersion < 8
and check as
if(iOS8)
{
}
else
{
}
Upvotes: 4
Reputation: 236558
Note: Available in iOS 8.0 and later. OS X v10.10 and later
var majorVersion: Int { return NSProcessInfo.processInfo().operatingSystemVersion.majorVersion }
var minorVersion: Int { return NSProcessInfo.processInfo().operatingSystemVersion.minorVersion }
var patchVersion: Int { return NSProcessInfo.processInfo().operatingSystemVersion.patchVersion }
var myOSVersion: String { return NSProcessInfo.processInfo().operatingSystemVersionString }
Upvotes: 1
Reputation: 1524
Mattt Thompson shares a very handy way
switch UIDevice.currentDevice().systemVersion.compare("8.0.0", options: NSStringCompareOptions.NumericSearch) {
case .OrderedSame, .OrderedDescending:
println("iOS >= 8.0")
case .OrderedAscending:
println("iOS < 8.0")
}
Upvotes: 3