我的Cocoa Objective-C应用程序如何查找Mac OS X版本号(例如“ 10.6.7”)?
我的Cocoa Objective-C应用程序如何查找Mac OS X版本号(例如“ 10.6.7”)?
#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];
你可以使用和苹果代码相同的技术...
NSDictionary *systemVersionDictionary =
[NSDictionary dictionaryWithContentsOfFile:
@"/System/Library/CoreServices/SystemVersion.plist"];
NSString *systemVersion =
[systemVersionDictionary objectForKey:@"ProductVersion"];
苹果正是通过函数_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 中新增的,因此在早期的答案编写时它并不存在。 - JWWalker