eug*_*ene 54 iphone simulator detect ios
我想知道我的应用程序是否在运行时在设备或模拟器上运行.有没有办法检测到这个?
用模拟器测试蓝牙api的原因:http: //volcore.limbicsoft.com/2009/09/iphone-os-31-gamekit-pt-1-woooohooo.html
vis*_*kh7 113
#if TARGET_OS_SIMULATOR
//Simulator
#else
// Device
#endif
Run Code Online (Sandbox Code Playgroud)
请参考前面提到的问题.在编译iPhone时,Xcode会设置什么#defines
Fer*_*tes 17
我创建了一个宏,您可以在其中指定要在括号内执行的操作,并且只有在模拟设备时才会执行这些操作.
#define SIM(x) if ([[[UIDevice currentDevice].model lowercaseString] rangeOfString:@"simulator"].location != NSNotFound){x;}
Run Code Online (Sandbox Code Playgroud)
这是这样使用的:
SIM(NSLog(@"This will only be logged if the device is simulated"));
Run Code Online (Sandbox Code Playgroud)
TARGET_IPHONE_SIMULATOR在设备上定义(但定义为false).并定义如下
#if TARGET_IPHONE_SIMULATOR
NSString * const DeviceMode = @"Simulator";
#else
NSString * const DeviceMode = @"Device";
#endif
Run Code Online (Sandbox Code Playgroud)
只是DeviceMode用来了解设备和模拟器之间的关系
检查模拟器
#if TARGET_IPHONE_SIMULATOR
// Simulator
#endif
Run Code Online (Sandbox Code Playgroud)
检查设备
#if !(TARGET_IPHONE_SIMULATOR)
// Device
#endif
Run Code Online (Sandbox Code Playgroud)
检查两者
#if TARGET_IPHONE_SIMULATOR
// Simulator
#else
// Device
#endif
Run Code Online (Sandbox Code Playgroud)
请注意,您不应该ifdef开启,
TARGET_IPHONE_SIMULATOR因为它将始终定义为1或0.