簡體   English   中英

如何在運行時獲取 iOS 設備 CPU 架構

[英]How can I get the iOS device CPU architecture in runtime

有沒有辦法在運行時識別 iOS 設備 CPU 架構?

謝謝你。

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

我認為這是更好的方法,

#import <mach-o/arch.h>

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

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

這是@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?
  • Mac Mini M1
    • iOS 13 模擬器: Intel 80486
    • iOS 14 模擬器: ARM64E
  • MacBook Pro 16" x86_64 英特爾:
    • iOS 13 模擬器:``(我這里沒有下載模擬器)
    • iOS 14 模擬器: Intel x86-64h Haswell

隨意更新這個。

替代答案是 Swift Package Manager 兼容:

public func getMachineHardwareName() -> String {
    #if arch(arm)
        return "arm"
    #elseif arch(arm64)
        return "arm64"
    #elseif arch(i386)
        return "i386"
    #elseif arch(powerpc64)
        return "powerpc64"
    #elseif arch(x86_64)
        return "x86_64"
    #elseif arch(s390x)
        return "s390x"
    #elseif arch(wasm32)
        return "wasm32"
    #else
        return "unknown_machine_architecture"
    #endif
}

我覺得這是 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM