How to know what Mac OS the app is running on? How to know what Mac OS the app is running on? xcode xcode

How to know what Mac OS the app is running on?


You're probably asking the wrong question. Except in very rare cases, you should not care what system version the user is running. Instead, you should be checking if the specific thing you're interested in is available.

For instance, if Apple introduces a MagicHologram class in Mac OS X 10.9 that you want to use, you don't check if the user is running Mac OS X 10.9. Instead, you check if the MagicHologram class is available. If it is, you can use it. If not, it's not available. It doesn't even matter why. Maybe they're running 10.8. But maybe it's five years later, and Apple's decided to drop the MagicHologram class entirely.

(Also, keep in mind that you'd need to weak link to HologramKit, the library that provides the MagicHologram class.)

Likewise, if they introduce a new method to NSString, instead of checking the OS version you'd check if NSString knows about the new method.

That said, NSApplication.h includes an external constant called NSAppKitVersionNumber. You can compare this to constants like NSAppKitVersionNumber10_7 which (it should be noted) are numbers like 1138, not 10.7. There's only a few places this is appropriate, mostly where classes were private and undocumented but got major changes before being documented and becoming a part of the public parts of the SDK. Also, it might be helpful if you want to avoid a specific bug that's been fixed since.

To recap:

  1. Detect individual classes and methods, which should cover 99.44% of your cases.
  2. Use NSAppKitVersionNumber and NSAppKitVersionNumber10_7 to cover those cases where class or method detection would lie to you.
  3. Those first two points cover all normal cases. You should go no further. But if you must have behaviour based on humane version, look at abarnert's answer below. It's the sanest way to get them.
  4. Don't use operatingSystemVersionString, which is specifically listed as not safe for parsing.

References/more information:

  • SDK Compatibility Guide "Read this document if you want your application to target a specific version or multiple versions of iOS or Mac OS X."
    • Using SDK-Based Development Describes how to use weakly linked classes, methods, and functions to support running on multiple versions of an operating system.


A quick way to do it is:

if ( NSAppKitVersionNumber >= NSAppKitVersionNumber10_7 ) {    // Do stuff for Lion or later}

More here.

You can see all the constants available in NSApplication.h, which you can get to by using Open Quickly... (Cmd-Shift O) in Xcode.

The header files provide for some surprisingly interesting reading material, if you are so inclined.


As others have said above (and I'd pick Steven Fisher's answer), you usually do not actually want to get the version number.

And if you only need to do comparisons against a major OS X version up to the version of the current SDK you're using, NSAppKitVersionNumber (as in Monolo's answer) is the right way to do it.

If you actually do need to get the version number for some reason (e.g., for recording analytics about your users, so you can decide when to stop supporting 10.6.0-10.6.5), here's how to do it:

#import <CoreServices/CoreServices.h>SInt32 majorVersion, minorVersion, bugFixVersion;Gestalt(gestaltSystemVersionMajor, &majorVersion);Gestalt(gestaltSystemVersionMinor, &minorVersion);Gestalt(gestaltSystemVersionBugFix, &bugFixVersion);  

For 10.7.3, this gives majorVersion = 10, minorVersion = 7, bugFixVersion = 3.

The 10.7 documentation removed the paragraph that directly suggested Gestalt as the way to get OS version, but it's still not deprecated or legacy, and there's no other suggestions. In fact, every other way to get this information (parsing -[NSProcessInfo operatingSystemVersionString], calling sysctlbyname on "kern.osrelease" and converting Darwin kernel version to OS X version, etc.) is explicitly counter-indicated somewhere. So, this is the way to do it, if you really want to.

Just keep in mind that, as the release notes for System 6.0.4 said back in 1989, this new API may not be permanent and could be removed in a future version of the OS.