Gergely Gergely - 7 months ago 26
Swift Question

Check OS version in SWIFT

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:


  1. What should be sysData's initial value? This example gives -1 in retVal probably because sysData is nil.

  2. How can I read information from sysData?


Answer

It depends on the system.

For iOS, try:

var systemVersion = UIDevice.currentDevice().systemVersion;

For OS X, try:

var systemVersion = NSProcessInfo.processInfo().operatingSystemVersion;

BUT it is not recommended to check the OS version. Better check if the feature you want to use is available on the device then 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 {
    //something else
}
Comments