55

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

Julio Gorgé
  • 10,056
  • 2
  • 45
  • 60
eugene
  • 39,839
  • 68
  • 255
  • 489
  • No need for checking for that at runtime, just use the TARGET_IPHONE_SIMULATOR definition to distinguish that case from the running on device case. – Till Apr 25 '11 at 05:24

8 Answers8

115
#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

Community
  • 1
  • 1
visakh7
  • 26,380
  • 8
  • 55
  • 69
  • 9
    Note that these are compile time macros and not available at runtime. – Eric Aug 01 '12 at 23:47
  • 1
    #include "TargetConditionals.h" // if your source is in c – Scott Stensland Nov 17 '12 at 14:57
  • 10
    Does the StackOverflow crowd have a reading comprehension problem? The question was to ask about distinguishing at *runtime* not at compile time! The right answer is the SIM macro by Fernando Cervantes, and not this one. Yet as of this comment writing, this wrong answer has 32 points and the right one has but 3. – StCredZero Oct 29 '13 at 18:52
  • 4
    @StCredZero Perhaps that is because code compiled to run on the simulator can't run on a device and vice versa, so in the end it doesn't matter wether you do the check at compile time or at run time. – Johan Kool Mar 17 '14 at 04:14
  • 2
    @JohanKool It does matter in some cases, such as if you're writing a static library that's used in other people's apps. You don't have the luxury of compile-time checking, because your code has already been compiled. So your check needs to be at run-time. Case in point: Including a warning msg for the developer for this Xcode 8 simulator bug: https://forums.developer.apple.com/message/179846 – Rizwan Sattar Oct 03 '16 at 16:04
  • 1
    @RizwanSattar That doesn't make any sense. You need to compile two versions of the library anyways. One is for the simulator, which uses a different architecture than the physical devices. This is the correct answer. – Agop Jan 18 '17 at 13:17
18

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"));
Fernando Cervantes
  • 2,962
  • 2
  • 23
  • 34
5

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.

hfossli
  • 22,616
  • 10
  • 116
  • 130
5

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

Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
4

From XCode 9.3+ , Swift

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

Helps you to code against device type specific.

theapache64
  • 10,926
  • 9
  • 65
  • 108
  • That new macro is for swift only: https://developer.apple.com/library/content/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html#//apple_ref/doc/uid/TP40001051-CH1-DontLinkElementID_68 – stonedauwg May 11 '18 at 14:42
  • Yeah, i forgot to mention it. Post edited. tnx @stonedauwg – theapache64 May 11 '18 at 15:13
2

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

Community
  • 1
  • 1
Julio Gorgé
  • 10,056
  • 2
  • 45
  • 60
0

Use this below code:

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

Works for Swift 4 and Xcode 9.4.1

Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62
0

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;
    }
tsukimi
  • 1,605
  • 2
  • 21
  • 36