【发布时间】:2018-12-10 20:19:47
【问题描述】:
如何从我的 Cocoa Objective-C 应用程序中找到 Mac OS X 的版本号(例如“10.6.7”)?
【问题讨论】:
标签: objective-c cocoa macos
如何从我的 Cocoa Objective-C 应用程序中找到 Mac OS X 的版本号(例如“10.6.7”)?
【问题讨论】:
标签: objective-c cocoa macos
#import <CoreServices/CoreServices.h>
SInt32 major, minor, bugfix;
Gestalt(gestaltSystemVersionMajor, &major);
Gestalt(gestaltSystemVersionMinor, &minor);
Gestalt(gestaltSystemVersionBugFix, &bugfix);
NSString *systemVersion = [NSString stringWithFormat:@"%d.%d.%d",
major, minor, bugfix];
【讨论】:
您可以使用与 Apple 代码相同的技术...
NSDictionary *systemVersionDictionary =
[NSDictionary dictionaryWithContentsOfFile:
@"/System/Library/CoreServices/SystemVersion.plist"];
NSString *systemVersion =
[systemVersionDictionary objectForKey:@"ProductVersion"];
Apple 正是这样做来在函数_CFCopySystemVersionDictionary 中填写各种系统实用程序的版本号:
【讨论】:
对于 OS X 10.10+,我认为使用 NSProcessInfo 是一种更简单、更安全的方法:
NSOperatingSystemVersion version = [[NSProcessInfo processInfo] operatingSystemVersion];
NSLog([NSString stringWithFormat:@"%ld.%ld.%ld", version.majorVersion, version.minorVersion, version.patchVersion]);
【讨论】:
operatingSystemVersion 方法是 OS X 10.10 的新方法,因此在编写早期答案时它并不存在。