Detecting the iOS device hardware architecture (32-bit/64-bit)

Posted: October 9th, 2013 | Author: | Filed under: iOS, Programming | Tags: , , , , , | 2 Comments »

In a previous post I explained how to detect if an app runs in a 32-bit or 64-bit iOS Simulator. It was not explaining how to detect if an iOS app runs on a 32-bit or 64-bit iOS device. This post aims at giving a generic method that can detect all cases:

  • 32-bit application running in a 32-bit iOS Simulator
  • 32-bit application running in a 64-bit iOS Simulator
  • 64-bit application running in a 64-bit iOS Simulator
  • 32-bit application running in a 32-bit iOS device
  • 32-bit application running in a 64-bit iOS device
  • 64-bit application running in a 64-bit iOS device

Below is the method is64bitHardware. It returns YES if the hardware is a 64-bit hardware and works on a real iOS device and in an iOS Simulator.
When running in an iOS Simulator, it uses the function is64bitSimulator() from my previous post http://blog.timac.org/?p=886.
When running on a real iOS device, it asks the kernel for the host info.


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