以编程方式检测应用程序是否正在设备或模拟器上运行
我想知道我的应用程序在运行时是在设备还是模拟器上运行。有没有办法检测到这一点?
原因是用模拟器测试蓝牙 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
请参考这个之前的SO问题设置了什么#defines为 iPhone 编译时由 Xcode 启动
Pls refer this previous SO question also What #defines are set up by Xcode when compiling for iPhone
我创建了一个宏,您可以在其中指定要在括号内执行的操作,并且这些操作仅在模拟设备时才会执行。
这是这样使用的:
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.
This is used like this:
TARGET_IPHONE_SIMULATOR 在设备上定义(但定义为 false)。定义如下
只需使用
DeviceMode
即可了解设备和模拟器TARGET_IPHONE_SIMULATOR is defined on the device (but defined to false). and defined as below
Just use
DeviceMode
to know between device and simulator检查是否模拟器
检查是否设备
检查两者
请注意,您不应该
ifdef
onTARGET_IPHONE_SIMULATOR
因为它始终被定义为1
或0
。Check if simulator
Check if device
Check for both
Please note that you should not
ifdef
onTARGET_IPHONE_SIMULATOR
because it will always be defined to either1
or0
.从 XCode 9.3+ 开始,Swift
帮助您针对特定设备类型进行编码。
From XCode 9.3+ , Swift
Helps you to code against device type specific.
您可以使用 TARGET_IPHONE_SIMULATOR 预处理器宏来区分设备和模拟器目标。
You can use the TARGET_IPHONE_SIMULATOR preprocessor macro to distinguish between device and simulator targets.
如果有人正在寻找 Unity 解决方案,我就是这么做的,这是我找到的唯一方法。
if anyone is looking for
Unity
solution i did this, the only way i found how.使用下面的代码:
适用于
Swift 4
和Xcode 9.4.1
Use this below code:
Works for
Swift 4
andXcode 9.4.1