简体   繁体   English

在Xcode iPhone项目中检测我是否正在为模拟器或设备构建?

[英]Detect in Xcode iPhone project whether I'm building for simulator or device?

Is there any way I can conditionally compile in my app based upon whether I'm building for the simulator or the device? 有没有什么方法可以根据我是在为模拟器还是设备构建而在我的应用程序中有条件地编译? (My app hooks to an external server: if I'm running on the device, I want to connect to localhost; if I'm running on the device, I want to go to my production server.) (我的应用程序挂钩到外部服务器:如果我在设备上运行,我想连接到localhost;如果我在设备上运行,我想去我的生产服务器。)

I'm looking for some #ifdef variable I can detect, or even something at runtime...doesn't matter. 我正在寻找一些我可以检测到的#ifdef变量,甚至是运行时的东西...无所谓。

Thanks. 谢谢。

#if !(TARGET_IPHONE_SIMULATOR)

or, alternatively, 或者,或者,

#if (TARGET_OS_IPHONE)

will tell you if you're running on the device. 会告诉你是否在设备上运行。 In order for it to work, you must 为了使它工作,你必须

#include "TargetConditionals.h"

file that you can find here . 你可以在这里找到的文件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM