确定iOS设备是32位还是64位

Eri*_*ill 30 ios

有谁知道一个简单的方法来判断iOS7设备是否具有32位或64位硬件?我不是指编程方式,我只是指通过设置,型号,第三方应用程序等.

我有一个问题,我怀疑是64位相关的.Apple的建议是在64位模拟器上进行测试,但也要在实际的64位设备上进行测试,但后来没有说明如何确定.我可以编写一个测试应用来检查sizeof(int)或者其他什么,但是必须有一些方法可以让技术支持知道他们正在使用什么.

埃里克

Nik*_* M. 37

没有其他"官方"方式来确定它.您可以使用以下代码确定它:

if (sizeof(void*) == 4) {
    NSLog(@"32-bit App");
} else if (sizeof(void*) == 8) {
    NSLog(@"64-bit App");
}
Run Code Online (Sandbox Code Playgroud)

  • 这将检查*application*是以32位还是64位模式运行,而不是*hardware*是32位还是64位. (9认同)
  • 根据Apple的说法,当您编译64位时,.ipa文件中还包含32位和64位二进制文​​件.因此,如果应用程序以64位模式运行,则硬件为64位. (4认同)
  • @NikosM.:是的,但是32位应用程序也可以在64位硬件上运行.(没有downvote btw.) (2认同)
  • 如果应用程序编译为32/64位"通用"二进制文件,则可以使用两个答案,因为64位片段将在64位硬件上运行,而32位片段将在32位硬件上运行.我只想指出`sizeof(void*)`或`#ifdef __LP64__`由编译器决定,而不是由硬件决定. (2认同)

Dra*_*gos 10

以下是方法is64bitHardware.如果硬件是64位硬件并且在真实iOS设备和iOS模拟器中工作,则返回YES.这是来源.

#include <mach/mach.h>

+ (BOOL) is64bitHardware
{
#if __LP64__
    // The app has been compiled for 64-bit intel and runs as 64-bit intel
    return YES;
#endif

    // Use some static variables to avoid performing the tasks several times.
    static BOOL sHardwareChecked = NO;
    static BOOL sIs64bitHardware = NO;

    if(!sHardwareChecked)
    {
        sHardwareChecked = YES;

#if TARGET_IPHONE_SIMULATOR
        // The app was compiled as 32-bit for the iOS Simulator.
        // We check if the Simulator is a 32-bit or 64-bit simulator using the function is64bitSimulator()
        // See http://blog.timac.org/?p=886
        sIs64bitHardware = is64bitSimulator();
#else
        // The app runs on a real iOS device: ask the kernel for the host info.
        struct host_basic_info host_basic_info;
        unsigned int count;
        kern_return_t returnValue = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)(&host_basic_info), &count);
        if(returnValue != KERN_SUCCESS)
        {
            sIs64bitHardware = NO;
        }

        sIs64bitHardware = (host_basic_info.cpu_type == CPU_TYPE_ARM64);

#endif // TARGET_IPHONE_SIMULATOR
    }

    return sIs64bitHardware;
}
Run Code Online (Sandbox Code Playgroud)

  • 如果您复制完整的功能,请链接到您的[来源](http://blog.timac.org/?p=907). (7认同)

Dar*_*ust 7

完全未经测试,但您应该能够通过以下方式获取CPU sysctl:

#include <sys/types.h>
#include <sys/sysctl.h>
#include <mach/machine.h>

void foo() {
    size_t size;
    cpu_type_t type;

    size = sizeof(type);
    sysctlbyname("hw.cputype", &type, &size, NULL, 0);

    if (type == CPU_TYPE_ARM64) {
        // ARM 64-bit CPU
    } else if (type == CPU_TYPE_ARM) {
        // ARM 32-bit CPU
    } else {
        // Something else.
    }
}
Run Code Online (Sandbox Code Playgroud)

在iOS 7 SDK中,CPU_TYPE_ARM64定义<mach/machine.h>如下:

#define CPU_TYPE_ARM64          (CPU_TYPE_ARM | CPU_ARCH_ABI64)
Run Code Online (Sandbox Code Playgroud)

一种不同的方式似乎是:

#include <mach/mach_host.h>

void foo() {
    host_basic_info_data_t hostInfo;
    mach_msg_type_number_t infoCount;

    infoCount = HOST_BASIC_INFO_COUNT;
    host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo, &infoCount);

    if (hostInfo.cpu_type == CPU_TYPE_ARM64) {
        // ARM 64-bit CPU
    } else if (hostInfo.cpu_type == CPU_TYPE_ARM) {
        // ARM 32-bit CPU
    } else {
        // Something else.
    }
}
Run Code Online (Sandbox Code Playgroud)


gns*_*ank 6

如果您正在使用铛编译,还有另一种方法:只需检查__arm____arm64__定义.

下面的示例代码没有经过测试,但它应该说明我的意思:

#if defined(__arm__)
    NSLog(@"32-bit App");
#elif defined(__arm64__)
    NSLog(@"64-bit App");
#else
    NSLog(@"Not running ARM");
#endif
Run Code Online (Sandbox Code Playgroud)

请注意,这取决于当前iOS应用程序二进制文件在单个容器中包含32位和64位二进制文​​件的事实,并且将根据您的应用程序是否支持执行64位而正确选择它们.


jhe*_*erg 5

您可以bitWidthInt https://developer.apple.com/documentation/swift/int/2885648-bitwidth上使用

static var is32Bit: Bool {
    return Int.bitWidth == 32
}

static var is64Bit: Bool {
    return Int.bitWidth == 64
}
Run Code Online (Sandbox Code Playgroud)