【问题标题】:How can I get the iOS device CPU architecture in runtime如何在运行时获取 iOS 设备 CPU 架构
【发布时间】:2013-11-20 11:34:47
【问题描述】:

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

谢谢。

【问题讨论】:

  • 尝试为设备型号设置定义:stackoverflow.com/questions/11197509/…
  • 谢谢,但我想我不是很清楚,或者我误导了这个问题,只要它检测到模型就可以了,如果它是 ARMv6、ARM、ARMv7、i386,我怎么能得到?
  • 嗯我想我明白你的意思了,我应该得到模型并设置定义,因为模型要找到架构。

标签: ios objective-c cpu-architecture


【解决方案1】:

您可以使用 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];
}

【讨论】:

  • 对于 iPhone 5s 或更高版本的设备,大多数设备都支持 64 位,我们可以添加 CPU_TYPE_ARM64 作为类型和子类型的新案例。
  • @Emmanuel 谢谢你的回答。但我想知道我们是否可以得到处理器的名称,如“Intel i5”
  • @VikasBansal 嗨,使用带有“machdep.cpu.brand_string”的 sysctlbyname 应该可以满足您的需求。 Here a complete answer
【解决方案2】:

只是在@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; 
  }

【讨论】:

  • 谢谢!您的答案是缺少 CPU_TYPE_ARM64 类型检查,随后可以对 CPU_SUBTYPE_ARM64_V8 进行子类型检查(目前它始终是 V8,但将来可能会改变)
【解决方案3】:

我认为这是更好的方法,

#import <mach-o/arch.h>

NXArchInfo *info = NXGetLocalArchInfo();
NSString *typeOfCpu = [NSString stringWithUTF8String:info->description];
//typeOfCpu = "arm64 v8"

【讨论】:

  • 如何快速翻译?
  • 非常适合我(我认为这是 2020 年最好的解决方案......)。谢谢:)
【解决方案4】:

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

import MachO

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

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

结果

  • iPhone 12:ARM64EWhat is ARM64E?
  • Mac Mini M1
    • iOS 13 模拟器:Intel 80486
    • iOS 14 模拟器:ARM64E
  • MacBook Pro 16" x86_64 英特尔:
    • iOS 13 模拟器:``(我这里没有下载模拟器)
    • iOS 14 模拟器:Intel x86-64h Haswell

随时更新。

【讨论】:

    【解决方案5】:

    我觉得这是 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

    【讨论】:

      猜你喜欢
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-04
      • 1970-01-01
      • 2021-11-03
      • 1970-01-01
      相关资源
      最近更新 更多