Programmatically detect if app is being run on device or simulator

IphoneIosSimulatorDetect

Iphone Problem Overview


I'd like to know whether my app is being run on device or simulator at run time. Is there a way to detect this?

Reason being to test bluetooth api with simulator: http://volcore.limbicsoft.com/2009/09/iphone-os-31-gamekit-pt-1-woooohooo.html

Iphone Solutions


Solution 1 - Iphone

#if TARGET_OS_SIMULATOR

//Simulator

#else

// Device

#endif

Pls refer this previous SO question also https://stackoverflow.com/questions/146986/what-defines-are-setup-by-xcode-when-compiling-for-iphone

Solution 2 - Iphone

I created a macro in which you can specify which actions you want to perform inside parentheses and these actions will only be performed if the device is being simulated.

#define SIM(x) if ([[[UIDevice currentDevice].model lowercaseString] rangeOfString:@"simulator"].location != NSNotFound){x;}

This is used like this:

SIM(NSLog(@"This will only be logged if the device is simulated"));

Solution 3 - Iphone

TARGET_IPHONE_SIMULATOR is defined on the device (but defined to false). and defined as below

#if TARGET_IPHONE_SIMULATOR
NSString * const DeviceMode = @"Simulator";
#else
NSString * const DeviceMode = @"Device";
#endif

Just use DeviceMode to know between device and simulator

Solution 4 - Iphone

Check if simulator

#if TARGET_IPHONE_SIMULATOR
// Simulator
#endif

Check if device

#if !(TARGET_IPHONE_SIMULATOR)
// Device
#endif

Check for both

#if TARGET_IPHONE_SIMULATOR
// Simulator
#else
// Device
#endif

Please note that you should not ifdef on TARGET_IPHONE_SIMULATOR because it will always be defined to either 1 or 0.

Solution 5 - Iphone

From XCode 9.3+ , Swift

#if targetEnvironment(simulator)
//Simulator
#else
//Real device
#endif

Helps you to code against device type specific.

Solution 6 - Iphone

You can use the TARGET_IPHONE_SIMULATOR preprocessor macro to distinguish between device and simulator targets.

Solution 7 - Iphone

Use this below code:

#if targetEnvironment(simulator)
   // iOS Simulator
#else
   // Device
#endif

Works for Swift 4 and Xcode 9.4.1

Solution 8 - Iphone

if anyone is looking for Unity solution i did this, the only way i found how.

using System.Globalization;

public static bool IsArm() {
		return CultureInfo.InvariantCulture.CompareInfo.IndexOf(SystemInfo.processorType, "ARM", CompareOptions.IgnoreCase) >= 0;
	}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioneugeneView Question on Stackoverflow
Solution 1 - Iphonevisakh7View Answer on Stackoverflow
Solution 2 - IphoneFernando CervantesView Answer on Stackoverflow
Solution 3 - IphoneJhaliya - Praveen SharmaView Answer on Stackoverflow
Solution 4 - IphonehfossliView Answer on Stackoverflow
Solution 5 - Iphonetheapache64View Answer on Stackoverflow
Solution 6 - IphoneJulio GorgéView Answer on Stackoverflow
Solution 7 - IphoneHaroldo GondimView Answer on Stackoverflow
Solution 8 - IphonetsukimiView Answer on Stackoverflow