Cocoa: Get Darwin version at runtime
Asked Answered
A

2

2

How can I get the darwin version at runtime? I know that I can get the macOS or iOS version using [[NSProcessInfo processInfo] operatingSystemVersion], but I want the Darwin version.

There are tables on the internet linking macOS and iOS releases to a darwin version, but i want to implement this future-proof.

Avilla answered 27/12, 2016 at 18:9 Comment(0)
P
2

Use the uname() POSIX/BSD library function. You declare a variable of type struct utsname and pass in its address. The release field of the struct will contain a C string with the Darwin version number, such as "16.3.0". If you want the individual components as integers, you'll have to parse it yourself.

Parenthesize answered 27/12, 2016 at 19:29 Comment(0)
O
1

adding code to @Ken Thomases response

  var systemInfo = utsname()
  uname(&systemInfo)
  let machineMirror = Mirror(reflecting: systemInfo.release)
  let darvinVersionString = machineMirror.children.reduce("") { identifier, element in
    guard let value = element.value as? Int8,
      value != 0 else {
        return identifier
    }

    return identifier + String(UnicodeScalar(UInt8(value)))
  }

Sample response:

19.3.0

enter image description here

And here a list of available versions

Ovaritis answered 3/3, 2020 at 8:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.