Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 761
George Taskos Avatar asked Nov 08 '13 12:11

George Taskos


People also ask

What are the CPU architectures supported by IOS devices?

On macOS, two architecture are supported: x86_64 is the architecture of Intel's 64-bit CPUs, sometimes also simply referred to as x64 . It is the architecture for all Intel Macs shipped between 2005 and 2021. arm64 is the architecture used by newer Macs built on Apple Silicon, shipped in late 2020 and beyond.

What architecture does iPhone use?

The Apple A16 Bionic is a 64-bit ARM-based SoC that first appeared in the iPhone 14 Pro, unveiled on September 7, 2022.


2 Answers

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];
}
like image 78
Emmanuel Avatar answered Sep 29 '22 22:09

Emmanuel


I think this is the better way,

#import <mach-o/arch.h>

NXArchInfo *info = NXGetLocalArchInfo();
NSString *typeOfCpu = [NSString stringWithUTF8String:info->description];
//typeOfCpu = "arm64 v8"
like image 27
Mahmut Şahin Avatar answered Sep 29 '22 21:09

Mahmut Şahin