How can I get the iOS device CPU architecture in runtime

前端 未结 3 1523
后悔当初
后悔当初 2020-12-04 23:19

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

Thank you.

相关标签:
3条回答
  • 2020-12-04 23:28

    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; 
      }
    
    0 讨论(0)
  • 2020-12-04 23:30

    I think this is the better way,

    #import <mach-o/arch.h>
    
    NXArchInfo *info = NXGetLocalArchInfo();
    NSString *typeOfCpu = [NSString stringWithUTF8String:info->description];
    //typeOfCpu = "arm64 v8"
    
    0 讨论(0)
  • 2020-12-04 23:43

    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];
    }
    
    0 讨论(0)
提交回复
热议问题