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

时间:2013-11-20 18:49:22

标签: ios

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

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

埃里克

7 个答案:

答案 0 :(得分:36)

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

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

答案 1 :(得分:10)

下面的方法是64位硬件。如果硬件是64位硬件并且在真实iOS设备和iOS模拟器中工作,则返回YES。这是source

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

答案 2 :(得分:7)

完全未经测试,但您应该可以通过sysctl获取CPU,如下所示:

#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.
    }
}

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

#define CPU_TYPE_ARM64          (CPU_TYPE_ARM | CPU_ARCH_ABI64)

另一种方式似乎是:

#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.
    }
}

答案 3 :(得分:6)

如果您使用clang进行编译,还有另一种方法:只需检查是否定义了__arm____arm64__

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

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

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

答案 4 :(得分:4)

您可以bitWidth使用Int 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
}

答案 5 :(得分:3)

我在swift 4中使用它,不确定它是否是最好的解决方案,但它有效。

import requests
import facebook

token = access_token="your token goes here"
fb = facebook.GraphAPI(access_token=token)
limit = 1
offset = 0
data = {"q": "jacob",
        "type": "user",
        "fields": "id",
        "limit": limit,
        "offset": offset}
req = fb.request('/search', args=data, method='GET')

users = []
for item in req['data']:
    users.append(item["id"])

pag = req['paging']
while pag.get("next") is not None:
    offset += limit
    data["offset"] = offset
    req = fb.request('/search', args=data, method='GET')
    for item in req['data']:
        users.append(item["id"])
    pag = req.get('paging')
print users

答案 6 :(得分:2)

在运行时,你可以使用类似这样的东西

extension UIDevice {
    static let is64Bit = MemoryLayout<Int>.size == MemoryLayout<Int64>.size
}