【问题标题】:Programmatically detect Apple Silicon vs Intel CPU in a Mac App at Runtime在运行时以编程方式检测 Mac 应用程序中的 Apple Silicon 与 Intel CPU
【发布时间】:2021-12-06 00:28:56
【问题描述】:

在 Mac 应用中,我如何以编程方式(在运行时)检测应用当前是否在配备 Intel 或 Apple Silicon 处理器的 Mac 上运行?

【问题讨论】:

  • 目标 C 还是 Swift?你标记了两者。
  • 编译时可以检测到

标签: swift objective-c macos apple-silicon


【解决方案1】:

在Objective-C中,你可以使用系统uname函数。这相当于来自 shell 的uname -m

#include <sys/utsname.h>

NSString *GetMachineHardwareName(void) {
    struct utsname sysinfo;
    int retVal = uname(&sysinfo);
    if (EXIT_SUCCESS != retVal) return nil;
    
    return [NSString stringWithUTF8String:sysinfo.machine];
}

或在 Swift 中

func GetMachineHardwareName() -> String? {
    var sysInfo = utsname()
    let retVal = uname(&sysInfo)

    guard retVal == EXIT_SUCCESS else { return nil }

    return String(cString: &sysInfo.machine.0, encoding: .utf8)
}

对于最新型号的英特尔 Mac,这将返回 x86_64。对于 Apple Silicon,它返回 arm64

【讨论】:

  • 在 Swift 中你会怎么做呢?
  • 我编辑了答案以包含至少在 Swift 5.5 中工作的 Swift 版本。
猜你喜欢
  • 1970-01-01
  • 2021-02-13
  • 2021-03-16
  • 2018-12-01
  • 2021-05-30
  • 2021-09-09
  • 2020-12-09
  • 2021-06-04
  • 2015-07-21
相关资源
最近更新 更多