17

有没有办法在运行时识别 iOS 设备 CPU 架构?

谢谢你。

4

5 回答 5

26

您可以使用 sysctlbyname :

#include <sys/types.h>
#include <sys/sysctl.h>
#include <mach/machine.h>

NSString *getCPUType(void)
{
    NSMutableString *cpu = [[NSMutableString alloc] init];
    size_t size;
    cpu_type_t type;
    cpu_subtype_t subtype;
    size = sizeof(type);
    sysctlbyname("hw.cputype", &type, &size, NULL, 0);

    size = sizeof(subtype);
    sysctlbyname("hw.cpusubtype", &subtype, &size, NULL, 0);

    // values for cputype and cpusubtype defined in mach/machine.h
    if (type == CPU_TYPE_X86)
    {
            [cpu appendString:@"x86 "];
             // check for subtype ...

    } else if (type == CPU_TYPE_ARM)
    {
            [cpu appendString:@"ARM"];
            switch(subtype)
            {
                    case CPU_SUBTYPE_ARM_V7:
                    [cpu appendString:@"V7"];
                    break;
                    // ...
            }
    }
    return [cpu autorelease];
}
于 2013-11-08T12:58:08.277 回答
12

我认为这是更好的方法,

#import <mach-o/arch.h>

NXArchInfo *info = NXGetLocalArchInfo();
NSString *typeOfCpu = [NSString stringWithUTF8String:info->description];
//typeOfCpu = "arm64 v8"
于 2017-10-18T06:44:46.797 回答
6

只需在@Emmanuel 的回答中添加更多内容:

- (NSString *)getCPUType {
      NSMutableString *cpu = [[NSMutableString alloc] init];
      size_t size;
      cpu_type_t type;
      cpu_subtype_t subtype;
      size = sizeof(type);
      sysctlbyname("hw.cputype", &type, &size, NULL, 0);

      size = sizeof(subtype);
      sysctlbyname("hw.cpusubtype", &subtype, &size, NULL, 0);

      // values for cputype and cpusubtype defined in mach/machine.h
      if (type == CPU_TYPE_X86_64) {
          [cpu appendString:@"x86_64"];
      } else if (type == CPU_TYPE_X86) {
          [cpu appendString:@"x86"];
      } else if (type == CPU_TYPE_ARM) {
          [cpu appendString:@"ARM"];
          switch(subtype)
          {
              case CPU_SUBTYPE_ARM_V6:
                  [cpu appendString:@"V6"];
                  break;
              case CPU_SUBTYPE_ARM_V7:
                  [cpu appendString:@"V7"];
                  break;
              case CPU_SUBTYPE_ARM_V8:
                  [cpu appendString:@"V8"];
                  break;
          }
      }
      return cpu; 
  }
于 2016-07-18T15:47:57.490 回答
5

这是@Mahmut 答案的快速版本。

import MachO

private func getArchitecture() -> NSString {
    let info = NXGetLocalArchInfo()
    return NSString(utf8String: (info?.pointee.description)!)!
}

print(getArchitecture() ?? "No architecture found")

结果

  • iPhone 12:ARM64E 什么是 ARM64E?
  • 麦克迷你 M1
    • iOS 13 模拟器:Intel 80486
    • iOS 14 模拟器:ARM64E
  • MacBook Pro 16" x86_64 英特尔:
    • iOS 13 模拟器:``(我这里没有下载模拟器)
    • iOS 14 模拟器:Intel x86-64h Haswell

随时更新。

于 2021-01-27T11:19:23.883 回答
1

我觉得这是 Swift 的最佳答案:

import MachO

func getArch() -> String? {
    guard let archRaw = NXGetLocalArchInfo().pointee.name else {
        return nil
    }
    return String(cString: archRaw)
}
print("Current device architecture: \(getArch() ?? "Unknown Archetiture")")

对于 A11 (iPhone X, 8, 8+) 和更低的设备,上面的函数会返回arm64,但是对于 A12 (iPhone Xs, Xs Max, XR) 和更新的设备,它会返回arm64e

于 2021-11-27T11:50:10.997 回答