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"
-
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 Answers
NSProcessInfo *pInfo = [NSProcessInfo processInfo];
NSString *version = [pInfo operatingSystemVersionString];
Sorry for the formatting, I'm using my iPad to answer this.

- 7,346
- 2
- 37
- 34

- 46,381
- 14
- 112
- 137
-
2That'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
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];

- 3,143
- 1
- 24
- 28
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

- 3,148
- 1
- 26
- 43
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.

- 95,783
- 15
- 211
- 370
-
3
-
1..with no decent replacement that would be as comprehensive as Gestalt is/was :-( – Jay Feb 11 '13 at 08:36
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):
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

- 2,741
- 2
- 22
- 38