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

查看:16
本文介绍了如何以编程方式确定我的应用程序是否在 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.

Xcode 设置什么 #defines为 iPhone 编译

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

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天全站免登陆