如何以编程方式确定我的应用程序是否在iphone模拟器中运行? [英] How can I programmatically determine if my app is running in the iphone simulator?

查看:88
本文介绍了如何以编程方式确定我的应用程序是否在iphone模拟器中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如问题所述,我主要想知道我的代码是否在模拟器中运行,但也有兴趣了解正在运行或正在模拟的特定iphone版本。

As the question states, I would mainly like to know whether or not my code is running in the simulator, but would also be interested in knowing the specific iphone version that is running or being simulated.

编辑:我在问题名称中添加了以编程方式这个词。我的问题是能够动态地包含/排除代码,具体取决于正在运行的版本/模拟器,所以我真的在寻找类似于预处理器指令的东西,它可以为我提供这些信息。

I added the word 'programmatically' to the question name. The point of my question is to be able to dynamically include / exclude code depending on which version / simulator is running, so I'd really be looking for something like a pre-processor directive that can provide me this info.

推荐答案

已经问过,但标题却截然不同。

Already asked, but with a very different title.

编译iPhone时Xcode设置了哪些#defines

我将从那里重复我的回答:

I'll repeat my answer from there:

它位于有条件地编译源代码下的SDK文档中

It's in the SDK docs under "Compiling source code conditionally"

相关定义是TARGET_OS_SIMULATOR,它在iOS框架中的/usr/include/TargetConditionals.h中定义。在早期版本的工具链中,你必须写:

The relevant definition is TARGET_OS_SIMULATOR, which is defined in /usr/include/TargetConditionals.h within the iOS framework. On earlier versions of the toolchain, you had to write:

#include "TargetConditionals.h"

但当前(Xcode 6 / iOS8)工具链不再需要这样做。

but this is no longer necessary on the current (Xcode 6/iOS8) toolchain.

因此,例如,如果你想检查你是否在设备上运行,你应该这样做

So, for example, if you want to check that you are running on device, you should do

#if TARGET_OS_SIMULATOR
    // Simulator-specific code
#else
    // Device-specific code
#endif

取决于哪种情况适合您的用例。

depending on which is appropriate for your use-case.

这篇关于如何以编程方式确定我的应用程序是否在iphone模拟器中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆