How would I be able开发者_StackOverflow中文版 to get the OSX version in objective-c? I would like to avoid using shell commands. E.g "10.5" or "10.4"
NSProcessInfo *pInfo = [NSProcessInfo processInfo];
NSString *version = [pInfo operatingSystemVersionString];
Sorry for the formatting, I'm using my iPad to answer this.
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];
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
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.
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?
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
精彩评论