12

How can I find the version number of Mac OS X (eg. "10.6.7") from my Cocoa Objective-C application?

AmaltasCoder
  • 1,123
  • 3
  • 17
  • 35
  • See also: http://stackoverflow.com/questions/157759/how-can-i-determine-the-running-mac-os-x-version-programmatically – Adam Rosenfield Oct 13 '11 at 19:07
  • possible duplicate of [How do I determine the OS version at runtime in OS X or iOS (without using Gestalt)?](http://stackoverflow.com/questions/11072804/how-do-i-determine-the-os-version-at-runtime-in-os-x-or-ios-without-using-gesta) – Jake Petroules Jun 06 '14 at 01:33

3 Answers3

25
#import <CoreServices/CoreServices.h>

SInt32 major, minor, bugfix;
Gestalt(gestaltSystemVersionMajor, &major);
Gestalt(gestaltSystemVersionMinor, &minor);
Gestalt(gestaltSystemVersionBugFix, &bugfix);

NSString *systemVersion = [NSString stringWithFormat:@"%d.%d.%d",
    major, minor, bugfix];
  • 6
    Be aware that this code is deprecated. Recommend checking in the linked article. – Brett Sep 10 '12 at 02:23
  • 1
    @Brett According to David Smith aka Catfish_Man, an engineer at Apple, it is fine to use Gestalt for version numbers. See [his tweet](https://twitter.com/Catfish_Man/status/373277120408997889). –  Oct 01 '13 at 10:38
  • 1
    Bavarious that is interesting, but it just goes to show that Apple's right hand doesn't always know what the left is doing. Code shouldn't be marked deprecated unless it is... – Brett Oct 01 '13 at 14:29
  • @Bavarious What if we want the actual name of the OSX e.g LION etc. Thank you – Vikas Bansal Jul 27 '15 at 15:46
19

You could use the same technique that Apple's code uses...

NSDictionary *systemVersionDictionary =
    [NSDictionary dictionaryWithContentsOfFile:
        @"/System/Library/CoreServices/SystemVersion.plist"];

NSString *systemVersion =
    [systemVersionDictionary objectForKey:@"ProductVersion"];

Apple does exactly this to fill in the version number for various system utilities in the function _CFCopySystemVersionDictionary here:

http://opensource.apple.com/source/CF/CF-744/CFUtilities.c

Matt Gallagher
  • 14,858
  • 2
  • 41
  • 43
  • Will this work on a sandboxed app, though? IMHO always better to use the system APIs.. – Jay Jan 31 '16 at 11:21
12

For OS X 10.10+ I think using NSProcessInfo is an easier and safer way to do that:

NSOperatingSystemVersion version = [[NSProcessInfo processInfo] operatingSystemVersion];
NSLog([NSString stringWithFormat:@"%ld.%ld.%ld", version.majorVersion, version.minorVersion, version.patchVersion]);
Jay
  • 6,572
  • 3
  • 37
  • 65
taichino
  • 1,135
  • 1
  • 15
  • 30
  • 1
    The `operatingSystemVersion` method is new with OS X 10.10, so it didn't exist when the earlier answers were written. – JWWalker Jun 09 '15 at 19:27