ios 如何以编程方式确定我的应用程序是否在 iphone 模拟器中运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/458304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:33:33  来源:igfitidea点击:

How can I programmatically determine if my app is running in the iphone simulator?

iosobjective-cswiftxcodeios-simulator

提问by Jeffrey Meyer

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.

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

EDIT: 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.

编辑:我在问题名称中添加了“以编程方式”这个词。我的问题的重点是能够根据正在运行的版本/模拟器动态包含/排除代码,所以我真的会寻找像预处理器指令这样的东西可以为我提供这些信息。

回答by Airsource Ltd

Already asked, but with a very different title.

已经问过了,但标题非常不同。

What #defines are set up by Xcode when compiling for iPhone

Xcode 在为 iPhone 编译时设置了什么 #defines

I'll repeat my answer from there:

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

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

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

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:

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

#include "TargetConditionals.h"

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

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

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.

取决于哪个适合您的用例。

回答by Pete

Updated code:

更新代码:

This is purported to work officially.

据说这是正式工作。

#if TARGET_IPHONE_SIMULATOR
NSString *hello = @"Hello, iPhone simulator!";
#elif TARGET_OS_IPHONE
NSString *hello = @"Hello, device!";
#else
NSString *hello = @"Hello, unknown target!";
#endif


Original post (since deprecated)

原始帖子(已弃用)

This code will tell you if you are running in a simulator.

此代码将告诉您是否在模拟器中运行。

#ifdef __i386__
NSLog(@"Running in the simulator");
#else
NSLog(@"Running on a device");
#endif

回答by Daniel Magnusson

Not pre-processor directive, but this was what I was looking for when i came to this question;

不是预处理器指令,但这正是我遇到这个问题时所寻找的;

NSString *model = [[UIDevice currentDevice] model];
if ([model isEqualToString:@"iPhone Simulator"]) {
    //device is simulator
}

回答by Taranfx

The best way to do this is:

最好的方法是:

#if TARGET_IPHONE_SIMULATOR

and not

并不是

#ifdef TARGET_IPHONE_SIMULATOR

since its always defined: 0 or 1

因为它总是定义:0 或 1

回答by Stefan Vasiljevic

THERE IS A BETTER WAY NOW!

现在有更好的方法!

As of Xcode 9.3 beta 4 you can use #if targetEnvironment(simulator)to check.

从 Xcode 9.3 beta 4 开始,您可以#if targetEnvironment(simulator)用来检查。

#if targetEnvironment(simulator)
//Your simulator code
#endif

UPDATE
Xcode 10 and iOS 12 SDK supports this too.

更新
Xcode 10 和 iOS 12 SDK 也支持这一点。

回答by Nischal Hada

In case of Swift we can implement following

在 Swift 的情况下,我们可以实现以下

We can create struct which allows you to create a structured data

我们可以创建允许您创建结构化数据的结构

struct Platform {
    static let isSimulator: Bool = {
        #if targetEnvironment(simulator)
            // we're on the simulator
            return true
        #else
            // we're on a device
             return false
        #endif
    }()
}

Then If we wanted to Detect if app is being built for device or simulator in Swift then .

然后,如果我们想检测应用程序是在 Swift 中为设备还是模拟器构建的,那么 .

if Platform.isSimulator {
    // Do one thing
} else {
    // Do the other
}

回答by Haroldo Gondim

Works for Swift 5and Xcode 11.3.1

适用于Swift 5Xcode 11.3.1

Use this code:

使用此代码:

#if targetEnvironment(simulator)
   // Simulator
#else
   // Device
#endif

回答by onmyway133

All those answer are good, but it somehow confuses newbie like me as it does not clarify compile check and runtime check. Preprocessor are before compile time, but we should make it clearer

所有这些答案都很好,但它以某种方式让像我这样的新手感到困惑,因为它没有阐明编译检查和运行时检查。预处理器在编译时间之前,但我们应该说得更清楚

This blog article shows How to detect the iPhone simulator?clearly

这篇博客文章展示了如何检测 iPhone 模拟器?清楚地

Runtime

运行

First of all, let's shortly discuss. UIDevice provides you already information about the device

首先,让我们简短地讨论一下。UIDevice 已为您提供有关设备的信息

[[UIDevice currentDevice] model]

will return you “iPhone Simulator” or “iPhone” according to where the app is running.

将根据应用程序运行的位置返回“iPhone Simulator”或“iPhone”。

Compile time

编译时间

However what you want is to use compile time defines. Why? Because you compile your app strictly to be run either inside the Simulator or on the device. Apple makes a define called TARGET_IPHONE_SIMULATOR. So let's look at the code :

但是,您想要的是使用编译时定义。为什么?因为您严格编译应用程序以在模拟器内或设备上运行。Apple 定义了一个名为 TARGET_IPHONE_SIMULATOR. 那么让我们看一下代码:

#if TARGET_IPHONE_SIMULATOR

NSLog(@"Running in Simulator - no app store or giro");

#endif

回答by CedricSoubrie

回答by Stunner

The previous answers are a little dated. I found that all you need to do is query the TARGET_IPHONE_SIMULATORmacro (no need to include any other header files[assuming you are coding for iOS]).

以前的答案有点过时了。我发现您需要做的就是查询TARGET_IPHONE_SIMULATOR宏(无需包含任何其他头文件[假设您正在为 iOS 编码])。

I attempted TARGET_OS_IPHONEbut it returned the same value (1) when running on an actual device and simulator, that's why I recommend using TARGET_IPHONE_SIMULATORinstead.

我尝试过,TARGET_OS_IPHONE但在实际设备和模拟器上运行时它返回相同的值 (1),这就是我建议改用的原因TARGET_IPHONE_SIMULATOR