Another quick tip: there are a bunch of defines which Apple provides you while your app is compiling and you can make great use of them. For example you whether the app will run on the device or in the simulator.
How to detect the iPhone simulator?
First of all, let’s shortly discuss. UIDevice provides you already information about the device
[[UIDevice currentDevice] model] |
will return you “iPhone Simulator” or “iPhone” according to where the app is running.
However what you want is to use compile time defines. Why? Because you compile your app strictly to be run either inside the Simulator or on the device. Apple makes a define called TARGET_IPHONE_SIMULATOR. So let’s look at the code :
#if TARGET_IPHONE_SIMULATOR NSLog(@"Running in Simulator - no app store or giro"); #endif |
It’s that easy. Now you can take care and maybe simulate some kind of input that lacks in the Simulator vs the device or other stuff you want to take care of.
NB: TARGET_OS_IPHONE
Now I’ve seen a ton of pages where some folks would just skim trough the Apple code find TARGET_OS_IPHONE and wrongly assume this define shows that the app is being compiled for the iPhone device. That’s wrong! I can’t believe there are even people complaining that Apple’s code is buggy because TARGET_OS_IPHONE is always 1.
Well let me put some light on that and please don’t make the same mistake. As the name of that define very clear explains this is a define which tells you whether the app is being compiled for the IPHONE OS ! Because even if most of the developers know only the iPhone development, actually XCode also provides development for the MAC OS X. So, if you are doing iPhone apps – yes! – TARGET_OS_IPHONE will be always 1.
How to detect the device
I feel this discussion will raise the question how to detect the device then. There’s 2 code samples you should use:
#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 |
So, hope this article was helpful. Please leave comments on ping me on Twitter.
And I have a message to some chinese web sites: please when you copy over my articles – AT LEAST include a working link back to my own web site and my name too !
The post was originally published on the following URL: http://www.touch-code-magazine.com/how-to-detect-running-on-iphone-simulator/
·



[...] This post was mentioned on Twitter by Marin Todorov, Marin Todorov. Marin Todorov said: #touchcodemagazine: How to detect the app is running on iPhone simulator http://bit.ly/dBKCnn [...]
Nice tip. Thanks.
You should mention that these are defined in TargetConditionals.h
I like doing the following in multiplatform code:
#ifdef __APPLE__
#include
#endif
…and my above comment is missing <TargetConditionals.h>. Ugh, XSS protection.