以编程方式检测应用程序是否正在设备或模拟器上运行

发布于 2024-11-03 10:53:10 字数 261 浏览 5 评论 0原文

我想知道我的应用程序在运行时是在设备还是模拟器上运行。有没有办法检测到这一点?

原因是用模拟器测试蓝牙 api: http://volcore.limbicsoft.com /2009/09/iphone-os-31-gamekit-pt-1-woooohooo.html

I'd like to know whether my app is being run on device or simulator at run time. Is there a way to detect this?

Reason being to test bluetooth api with simulator:
http://volcore.limbicsoft.com/2009/09/iphone-os-31-gamekit-pt-1-woooohooo.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

冷情妓 2024-11-10 10:53:10
#if TARGET_OS_SIMULATOR

//Simulator

#else

// Device

#endif

请参考这个之前的SO问题设置了什么#defines为 iPhone 编译时由 Xcode 启动

#if TARGET_OS_SIMULATOR

//Simulator

#else

// Device

#endif

Pls refer this previous SO question also What #defines are set up by Xcode when compiling for iPhone

仙女山的月亮 2024-11-10 10:53:10

我创建了一个宏,您可以在其中指定要在括号内执行的操作,并且这些操作仅在模拟设备时才会执行。

#define SIM(x) if ([[[UIDevice currentDevice].model lowercaseString] rangeOfString:@"simulator"].location != NSNotFound){x;}

这是这样使用的:

SIM(NSLog(@"This will only be logged if the device is simulated"));

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"));
肤浅与狂妄 2024-11-10 10:53:10

TARGET_IPHONE_SIMULATOR 在设备上定义(但定义为 false)。定义如下

#if TARGET_IPHONE_SIMULATOR
NSString * const DeviceMode = @"Simulator";
#else
NSString * const DeviceMode = @"Device";
#endif

只需使用 DeviceMode 即可了解设备和模拟器

TARGET_IPHONE_SIMULATOR is defined on the device (but defined to false). and defined as below

#if TARGET_IPHONE_SIMULATOR
NSString * const DeviceMode = @"Simulator";
#else
NSString * const DeviceMode = @"Device";
#endif

Just use DeviceMode to know between device and simulator

甜扑 2024-11-10 10:53:10

检查是否模拟器

#if TARGET_IPHONE_SIMULATOR
// Simulator
#endif

检查是否设备

#if !(TARGET_IPHONE_SIMULATOR)
// Device
#endif

检查两者

#if TARGET_IPHONE_SIMULATOR
// Simulator
#else
// Device
#endif

请注意,您不应该 ifdef on
TARGET_IPHONE_SIMULATOR 因为它始终被定义为 10

Check if simulator

#if TARGET_IPHONE_SIMULATOR
// Simulator
#endif

Check if device

#if !(TARGET_IPHONE_SIMULATOR)
// Device
#endif

Check for both

#if TARGET_IPHONE_SIMULATOR
// Simulator
#else
// Device
#endif

Please note that you should not ifdef on
TARGET_IPHONE_SIMULATOR because it will always be defined to either 1 or 0.

跨年 2024-11-10 10:53:10

从 XCode 9.3+ 开始,Swift

#if targetEnvironment(simulator)
//Simulator
#else
//Real device
#endif

帮助您针对特定设备类型进行编码。

From XCode 9.3+ , Swift

#if targetEnvironment(simulator)
//Simulator
#else
//Real device
#endif

Helps you to code against device type specific.

提赋 2024-11-10 10:53:10

您可以使用 TARGET_IPHONE_SIMULATOR 预处理器宏来区分设备和模拟器目标。

You can use the TARGET_IPHONE_SIMULATOR preprocessor macro to distinguish between device and simulator targets.

霞映澄塘 2024-11-10 10:53:10

如果有人正在寻找 Unity 解决方案,我就是这么做的,这是我找到的唯一方法。

using System.Globalization;

public static bool IsArm() {
        return CultureInfo.InvariantCulture.CompareInfo.IndexOf(SystemInfo.processorType, "ARM", CompareOptions.IgnoreCase) >= 0;
    }

if anyone is looking for Unity solution i did this, the only way i found how.

using System.Globalization;

public static bool IsArm() {
        return CultureInfo.InvariantCulture.CompareInfo.IndexOf(SystemInfo.processorType, "ARM", CompareOptions.IgnoreCase) >= 0;
    }
数理化全能战士 2024-11-10 10:53:10

使用下面的代码:

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

适用于 Swift 4Xcode 9.4.1

Use this below code:

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

Works for Swift 4 and Xcode 9.4.1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文