alexyorke
alexyorke

Reputation: 4319

Get OSX version with objective-c

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"

Upvotes: 8

Views: 9908

Answers (6)

Jenn
Jenn

Reputation: 3533

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];

Upvotes: 10

Ferrakkem Bhuiyan
Ferrakkem Bhuiyan

Reputation: 2783

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

Upvotes: -1

ennuikiller
ennuikiller

Reputation: 46985

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

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

Upvotes: 21

Jay
Jay

Reputation: 6638

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?

Upvotes: 1

Shebuka
Shebuka

Reputation: 3228

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

Upvotes: 4

Peter Hosey
Peter Hosey

Reputation: 96373

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.

Upvotes: 2

Related Questions