Programmatically determine if an application is running on a device or simulator

I would like to know if my application is running on a device or simulator at runtime. Is there any way to detect this?

The reason is to check the bluetooth api with a simulator: http://volcore.limbicsoft.com/2009/09/iphone-os-31-gamekit-pt-1-woooohooo.html

+50
ios iphone simulator detect
Apr 25 '11 at 5:18
source share
5 answers
#if TARGET_OS_SIMULATOR //Simulator #else // Device #endif 

Pls refer to this previous SO question also. Which #defines are configured by Xcode when compiling for iPhone

+106
Apr 25 '11 at 5:22
source share

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

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

This is used as follows:

 SIM(NSLog(@"This will only be logged if the device is simulated")); 
+17
Mar 29 '12 at 18:29
source share

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

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

Just use DeviceMode to know between the device and the simulator

+5
Apr 25 '11 at 5:28
source share

Make sure the simulator

 #if TARGET_IPHONE_SIMULATOR // Simulator #endif 

Make sure the device

 #if !(TARGET_IPHONE_SIMULATOR) // Device #endif 

Check how

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

Note that you should not ifdef on TARGET_IPHONE_SIMULATOR , because it will always be specified either 1 or 0 .

+5
Sep 11 '14 at 10:09
source share

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

+2
Apr 25 '11 at 5:22
source share



All Articles