eugene
eugene

Reputation: 41765

Programmatically detect if app is being run on device or simulator

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

Upvotes: 55

Views: 26412

Answers (8)

tsukimi
tsukimi

Reputation: 1645

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

Upvotes: 1

Haroldo Gondim
Haroldo Gondim

Reputation: 7993

Use this below code:

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

Works for Swift 4 and Xcode 9.4.1

Upvotes: 0

theapache64
theapache64

Reputation: 11744

From XCode 9.3+ , Swift

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

Helps you to code against device type specific.

Upvotes: 4

visakh7
visakh7

Reputation: 26400

#if TARGET_OS_SIMULATOR

//Simulator

#else

// Device

#endif

Pls refer this previous SO question also What #defines are set up by Xcode when compiling for iPhone

Upvotes: 115

hfossli
hfossli

Reputation: 22992

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.

Upvotes: 5

Fernando Cervantes
Fernando Cervantes

Reputation: 2962

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"));

Upvotes: 18

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31730

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

Upvotes: 5

Julio Gorgé
Julio Gorgé

Reputation: 10106

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

Upvotes: 2

Related Questions