How to check Mac OS X version at runtime

Parag Bafna picture Parag Bafna · Aug 12, 2015 · Viewed 9.2k times · Source

I am using below code to check OS X version at runtime.

if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_10)
{
    /* On a 10.10.x or earlier system */
}

But this condition return false on 10.10.4 OS X. I am using Xcode 6.3.2.

enter image description here

According to AppKit Release Notes for OS X v10.11, It should work.

if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_9) {
  /* On a 10.9.x or earlier system */
} else if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_10) {
  /* On a 10.10 - 10.10.x system */
} else {
  /* 10.11 or later system */
}

Answer

pkis picture pkis · Nov 16, 2017

Since Xcode 9.0, you can use code below. Reference to Apple documentation.

Swift

if #available(macOS 10.13, *) {
      // macOS 10.13 or later code path
} else {
      // code for earlier than 10.13
}

Objective-C

if (@available(macOS 10.13, *)) {
      // macOS 10.13 or later code path
} else {
      // code for earlier than 10.13
}