Programmatically detect if app is being run on device or simulator Programmatically detect if app is being run on device or simulator ios ios

Programmatically detect if app is being run on device or simulator


#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


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


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

#if TARGET_IPHONE_SIMULATORNSString * const DeviceMode = @"Simulator";#elseNSString * const DeviceMode = @"Device";#endif

Just use DeviceMode to know between device and simulator