有没有办法在运行时识别iOS设备CPU架构?
谢谢。
答案 0 :(得分:25)
您可以使用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];
}
答案 1 :(得分:4)
只需在@ 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;
}
答案 2 :(得分:4)
我认为这是更好的方式,
#import <mach-o/arch.h>
NXArchInfo *info = NXGetLocalArchInfo();
NSString *typeOfCpu = [NSString stringWithUTF8String:info->description];
//typeOfCpu = "arm64 v8"
答案 3 :(得分:4)
这是@Mahmut 答案的快速版本。
import MachO
private func getArchitecture() -> NSString {
let info = NXGetLocalArchInfo()
return NSString(utf8String: (info?.pointee.description)!)!
}
print(getArchitecture() ?? "No architecture found")
ARM64E
What is ARM64E?Intel 80486
ARM64E
Intel x86-64h Haswell
随时更新。