Determinar se o dispositivo iOS é de 32-ou 64-bits
Eric
7 answers
if (sizeof(void*) == 4) {
NSLog(@"32-bit App");
} else if (sizeof(void*) == 8) {
NSLog(@"64-bit App");
}
Abaixo está o método is64bitHardware. Ele retorna Sim se o hardware é um hardware de 64 bits e funciona em um dispositivo iOS real e em um simulador iOS. Aqui está Fonte .
#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;
}
Totalmente não testado, mas você deve ser capaz de obter a CPU via sysctl
Assim:
#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.
}
}
No iOS 7 SDK, CPU_TYPE_ARM64
está definido em <mach/machine.h>
como:
#define CPU_TYPE_ARM64 (CPU_TYPE_ARM | CPU_ARCH_ABI64)
Uma maneira diferente parece ser:
#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.
}
}
Se estiver a compilar com clang, existe outra forma: verifique se __arm__
ou __arm64__
está definido.
O código de exemplo abaixo não é testado, mas deve ilustrar o que quero dizer com isso:
#if defined(__arm__)
NSLog(@"32-bit App");
#elif defined(__arm64__)
NSLog(@"64-bit App");
#else
NSLog(@"Not running ARM");
#endif
Note que isto se baseia no facto de os binários de aplicação iOS actuais conterem ambos, 32 bits e 64 bits num único contentor e eles serão seleccionados correctamente, dependendo se a sua aplicação suporta a execução de 64 bits.
Pode utilizar bitWidth
em 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
}
Uso isto no swift 4, não sei se é a melhor solução, mas funciona.
func getCPUArch()
{
#if arch(arm)
print("this is a 32bit system")
#elseif arch(arm64)
print("this is a 64bit system")
#endif
}
Em tempo de execução pode usar algo como isto
extension UIDevice {
static let is64Bit = MemoryLayout<Int>.size == MemoryLayout<Int64>.size
}