25

Possible Duplicate:
Programmatically detect if app is being run on device or simulator

How can I detect whether my app is running on Simulator or on Device via code.

Community
  • 1
  • 1
user1173142
  • 553
  • 2
  • 6
  • 10
  • 2
    seen this?? http://stackoverflow.com/questions/5775420/programmatically-detect-if-app-is-being-run-on-device-or-simulator – Vin Mar 15 '12 at 10:41
  • 1
    duplicate of so many posts: http://stackoverflow.com/questions/5122149/iphone-simulator-how-to-detect-when-app-is-running-on-simulator-so-can-setup, http://stackoverflow.com/questions/5775420/programmatically-detect-if-app-is-being-run-on-device-or-simulator, http://stackoverflow.com/questions/458304/how-can-i-programmatically-determine-if-my-app-is-running-in-the-iphone-simulato – Yama Mar 15 '12 at 10:45
  • 1
    @Fulvio sorry if it sounded harsh. This question has been asked a lot on SO.Aren't we supposed to search already existing post, related to our question, before posting? – Vin Mar 15 '12 at 10:50
  • http://stackoverflow.com/questions/458304/how-can-i-programmatically-determine-if-my-app-is-running-in-the-iphone-simulato have a look – rashii Jan 07 '13 at 08:43

3 Answers3

84

Keep in mind UIDevice provides you already with information about the device itself.

[[UIDevice currentDevice] model]

You can also use the following:

TARGET_IPHONE_SIMULATOR tells you if you're in the iPhone simulator.

TARGET_OS_IPHONE tells you that you're working on the iPhone instead of MacOS.

#if TARGET_IPHONE_SIMULATOR

    NSLog(@"Running in Simulator - no app store or giro");

#else

    NSLog(@"Running on the Device");

#endif

and when ONLY interested in the device

#if !(TARGET_IPHONE_SIMULATOR)

    NSLog(@"Running on device");

#endif
Alejandro Luengo
  • 1,256
  • 1
  • 17
  • 27
gotnull
  • 26,454
  • 22
  • 137
  • 203
9

You can use this constant

#if TARGET_OS_SIMULATOR
    NSLog(@"This is simulator mode....");
#else
    NSLog(@"This is device mode....");
#endif
Logicsaurus Rex
  • 3,172
  • 2
  • 18
  • 27
imthi
  • 4,798
  • 1
  • 22
  • 24
1

The same compiled app cannot run on both the Simulator and an iOS device, as the CPU instructions sets are completely different (x86 vs. ARM). (...unless you are building some sort of very strange super-universal binary using lipo)

There are several ways to determine whether the app was compiled for x86 or not. One is to add run time code the differs depending on one of the many predefined compiler preprocessor macros. You can get a list of preprocessor macros for an x86 compile by typing this on the Terminal command line:

gcc -arch i386 -dM -E - < /dev/null | sort

hotpaw2
  • 70,107
  • 14
  • 90
  • 153