Get OSX version with objective-c

How do I get the OSX version in objective-c? I would like to avoid using shell commands. For example, "10.5" or "10.4"

+4
source share
6 answers
NSProcessInfo *pInfo = [NSProcessInfo processInfo]; NSString *version = [pInfo operatingSystemVersionString]; 

Sorry for the formatting, I am using my iPad to answer this question.

+15
source

Starting from 10.10, you can use NSProcessInfo.processInfo.operatingSystemVersion to get the NSOperatingSystemVersion structure.

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

There is also a useful method isOperatingSystemAtLeastVersion:

 NSOperatingSystemVersion minimumSupportedOSVersion = { .majorVersion = 10, .minorVersion = 12, .patchVersion = 0 }; BOOL isSupported = [NSProcessInfo.processInfo isOperatingSystemAtLeastVersion:minimumSupportedOSVersion]; 
+3
source

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

Older Gestalt users may be amazed to find that they are still available in 64-bit.

+2
source

You can analyze it in such a way as to get the desired format:

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

This fe will give you Mac OS X 10.6.8

+2
source

See this answer using NSAppKitVersionNumber if you are also using AppKit in your application (and want to run 10.8+ since Gestalt is now deprecated):

How do I find out what Mac OS works for?

+1
source

add this code after #import

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

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

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

you can easily get the OS version above the code

thanks

0
source

All Articles