Xcode,iPhone:如何在编译时检测模拟器目标?

Tho*_*ann 22 iphone xcode ios-simulator

我想知道,在为模拟器构建iPhone应用程序时,是否添加了特殊的DEFINE,允许我有条件地编译这种情况?

如果没有,我将不得不为这种情况添加我自己的目标,但我宁愿采用自动检测方法.

或者,是否有一种动态的方式来了解我的代码何时在模拟器上运行,我的意思是记录了什么?我一直在搜索文档一段时间,但还没有运气.

Vla*_*mir 54

对于编译时检查,您需要在TargetConditionals.h中定义TARGET_IPHONE_SIMULATOR

#if TARGET_IPHONE_SIMULATOR
// Simulator code
#endif
Run Code Online (Sandbox Code Playgroud)

对于运行时检查,您可以使用-modelUIDevice中的示例方法.对于iPhone模拟器,它返回iPhone Simulator字符串(虽然不确定iPad模拟器)


kau*_*hal 11

@Update:
在iOS 9.0 SDK中,TARGET_IPHONE_SIMULATOR为 - DEPRECATED.使用TARGET_OS_SIMULATOR而不是TARGET_IPHONE_SIMULATOR

#if TARGET_OS_SIMULATOR
  // Simulator code
#endif
Run Code Online (Sandbox Code Playgroud)

来自Swift 4.1:

#if targetEnvironment(simulator)
    // code for the simulator here
#else
   // code for real devices here
#endif
Run Code Online (Sandbox Code Playgroud)