[英]Get OSX version with objective-c
How would I be able to get the OSX version in objective-c? 我怎样才能在objective-c中获得OSX版本? I would like to avoid using shell commands. 我想避免使用shell命令。 Eg "10.5" or "10.4" 例如“10.5”或“10.4”
NSProcessInfo *pInfo = [NSProcessInfo processInfo];
NSString *version = [pInfo operatingSystemVersionString];
Sorry for the formatting, I'm using my iPad to answer this. 抱歉格式化,我正在使用我的iPad来回答这个问题。
As of 10.10 you can use NSProcessInfo.processInfo.operatingSystemVersion
to get a NSOperatingSystemVersion struct. 从10.10开始,您可以使用NSProcessInfo.processInfo.operatingSystemVersion
来获取NSOperatingSystemVersion结构。
typedef struct {
NSInteger majorVersion;
NSInteger minorVersion;
NSInteger patchVersion;
} NSOperatingSystemVersion;
There's also a helpful isOperatingSystemAtLeastVersion:
method. 还有一个有用的isOperatingSystemAtLeastVersion:
方法。
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 fe will give you Mac OS X 10.6.8
这个fe将为您提供Mac OS X 10.6.8
You can use the Gestalt
function to access the components of the OS version . 您可以使用Gestalt
函数访问OS版本的组件 。
Old-time users of Gestalt
may be amazed to find that it is still available in 64-bit. Gestalt
老用户可能会惊讶地发现它仍然可以在64位中使用。
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): 如果您在应用程序中使用AppKit,也可以使用NSAppKitVersionNumber
查看此响应(并希望在10.8+上运行,因为现在不推荐使用Gestalt):
How to know what Mac OS the app is running on? 如何知道该应用运行的Mac OS是什么?
add this code after #import
在#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 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.