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

有谁知道一个简单的方法来告诉如果iOS7设备有32位或64位硬件? 我不是以编程的方式,我的意思是通过设置,型号,第三方应用程序等。

我有一个问题,我怀疑是64位相关的。 苹果的build议是在64位模拟器上进行testing,但也要在实际的64位设备上进行testing,但是并没有说明如何确定这一点。 我可以编写一个testing应用程序来检查sizeof(int)或其他什么,但是有一些技术支持的方法可以知道他们在做什么。

埃里克

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

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

下面是方法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; } 

完全没有经过testing,但你应该可以像这样通过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. } } 

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

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

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

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

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

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

您可以在Int https://developer.apple.com/documentation/swift/int/2885648-bitwidth上使&#x7528;bitWidth

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

我用这个在迅速4,不知道这是最好的解决scheme,但它的工作原理。

  func getCPUArch() { #if arch(arm) print("this is a 32bit system") #elseif arch(arm64) print("this is a 64bit system") #endif }