【问题标题】:Find Mac OS X version number in objective c在目标 c 中查找 Mac OS X 版本号
【发布时间】:2018-12-10 20:19:47
【问题描述】:

如何从我的 Cocoa Objective-C 应用程序中找到 Mac OS X 的版本号(例如“10.6.7”)?

【问题讨论】:

标签: objective-c cocoa macos


【解决方案1】:
#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];

【讨论】:

  • 请注意,此代码已被弃用。建议查看链接的文章。
  • @Brett 根据 Apple 工程师 David Smith aka Catfish_Man 的说法,可以使用格式塔作为版本号。见his tweet
  • 这很有趣,但这只是表明苹果的右手并不总是知道左手在做什么。代码不应被标记为已弃用,除非它是...
  • @Bavarious 如果我们想要 OSX 的实际名称,例如 LION 等。谢谢
【解决方案2】:

您可以使用与 Apple 代码相同的技术...

NSDictionary *systemVersionDictionary =
    [NSDictionary dictionaryWithContentsOfFile:
        @"/System/Library/CoreServices/SystemVersion.plist"];

NSString *systemVersion =
    [systemVersionDictionary objectForKey:@"ProductVersion"];

Apple 正是这样做来在函数_CFCopySystemVersionDictionary 中填写各种系统实用程序的版本号:

http://opensource.apple.com/source/CF/CF-744/CFUtilities.c

【讨论】:

  • 这是否适用于沙盒应用程序?恕我直言,使用系统 API 总是更好..
【解决方案3】:

对于 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 的新方法,因此在编写早期答案时它并不存在。
猜你喜欢
  • 2015-03-20
  • 1970-01-01
  • 2019-07-19
  • 2023-03-22
  • 2012-06-01
  • 2011-01-02
  • 2020-10-30
  • 1970-01-01
相关资源
最近更新 更多