当前位置: 动力学知识库 > 问答 > 编程问答 >

iphone - Programmatically detect if app is being run on device or simulator

问题描述:

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

网友答案:
#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_SIMULATOR
NSString * const DeviceMode = @"Simulator";
#else
NSString * const DeviceMode = @"Device";
#endif

Just use DeviceMode to know between device and simulator

网友答案:

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.

网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: