简体   繁体   中英

How can I get the iOS device CPU architecture in runtime

Is there a way to identify the iOS device CPU architecture in runtime?

Thank you.

You can use 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];
}

I think this is the better way,

#import <mach-o/arch.h>

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

Just adding more to @Emmanuel's answer:

- (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; 
  }

Here is the swift version of @Mahmut's answer.

import MachO

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

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

Results

  • iPhone 12: ARM64E What is ARM64E?
  • Mac Mini M1
    • iOS 13 emulator: Intel 80486
    • iOS 14 emulator: ARM64E
  • MacBook Pro 16" x86_64 Intel:
    • iOS 13 emulator: `` (I don't have the emulators downloaded here)
    • iOS 14 emulator: Intel x86-64h Haswell

Feel free to update this.

Alternative answer that is Swift Package Manager compliant:

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
}

I feel that this is the best answer for Swift yet:

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")")

For A11 (iPhone X, 8, 8+) and lower devices, the function above will return arm64 , However for A12 (iPhone Xs, Xs Max, XR) and newer devices, it will return arm64e

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM