8

How would I be able to get the OSX version in objective-c? I would like to avoid using shell commands. E.g "10.5" or "10.4"

alexyorke
  • 4,224
  • 3
  • 34
  • 55
  • possible duplicate of [Mac OS X: Replacement for Gestalt() for testing OS version at runtime](http://stackoverflow.com/questions/11072804/mac-os-x-replacement-for-gestalt-for-testing-os-version-at-runtime) – Jake Petroules Jun 06 '14 at 01:25

6 Answers6

21
NSProcessInfo *pInfo = [NSProcessInfo processInfo];
NSString *version = [pInfo operatingSystemVersionString];

Sorry for the formatting, I'm using my iPad to answer this.

Chris Miles
  • 7,346
  • 2
  • 37
  • 34
ennuikiller
  • 46,381
  • 14
  • 112
  • 137
  • 2
    That's fine if you want to display the version to the user, but not fine if you want to do processing based on the OS version. – JeremyP Sep 12 '10 at 16:56
  • This works, but there is a lot of "garbage" text that is added with it (Build information etc). – alexyorke Sep 13 '10 at 09:42
  • 1
    @JeremyP there is method `operatingSystemVersion` gives you a struct with three integers, major, minor and patch version which might be easier to process – Samantha Catania Feb 12 '15 at 21:39
9

As of 10.10 you can use NSProcessInfo.processInfo.operatingSystemVersion to get a NSOperatingSystemVersion struct.

typedef struct {
    NSInteger majorVersion;
    NSInteger minorVersion;
    NSInteger patchVersion;
} NSOperatingSystemVersion;

There's also a helpful isOperatingSystemAtLeastVersion: method.

NSOperatingSystemVersion minimumSupportedOSVersion = { .majorVersion = 10, .minorVersion = 12, .patchVersion = 0 };
BOOL isSupported = [NSProcessInfo.processInfo isOperatingSystemAtLeastVersion:minimumSupportedOSVersion];
Jenn
  • 3,143
  • 1
  • 24
  • 28
4

You can parse it in this way to get the format you want:

NSProcessInfo *pinfo = [NSProcessInfo processInfo];

NSArray *myarr = [[pinfo operatingSystemVersionString] componentsSeparatedByString:@" "];
NSString *version = [@"Mac OS X " stringByAppendingString:[myarr objectAtIndex:1]];

This f.e. will give you Mac OS X 10.6.8

Shebuka
  • 3,148
  • 1
  • 26
  • 43
2

You can use the Gestalt function to access the components of the OS version.

Old-time users of Gestalt may be amazed to find that it is still available in 64-bit.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
1

See this response using NSAppKitVersionNumber in case you're using AppKit in your app as well (and want to run on 10.8+ as Gestalt is now deprecated):

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

Community
  • 1
  • 1
Jay
  • 6,572
  • 3
  • 37
  • 65
-1

add this code after #import

#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)  

after adding above code please add below code where you want to see your os-version

NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
        NSLog(@"System version :%@",systemVersion);

you can easily get OS-version by above code

Thank you

Ferrakkem Bhuiyan
  • 2,741
  • 2
  • 22
  • 38