简体   繁体   English

如何确定我的Cordova应用程序是在模拟器还是真实设备上运行?

[英]How can I tell if my Cordova application is running on a simulator or real device?

I'd like to be talk to a different server when running in an emulator / simulator. 我想在仿真器/模拟器中运行时与其他服务器通信。

It looks like device.platform used to mention the simulator, but know it just says "iOS". 看起来好像device.platform曾经提到模拟器,但知道它只是说“ iOS”。 The user-agent doesn't seem to differentiate either. 用户代理似乎也没有区别。

Ideally the solution would work for all platforms, but I'll take Android and iOS, or in fact anything helpful! 理想情况下,该解决方案适用于所有平台,但我将使用Android和iOS,或者实际上有用的任何东西!

For iOS I just check the model - if matches /x86/ then it's the simulator (until Apple release an Intel iOS device). 对于iOS,我只需检查模型-如果与/ x86 /匹配,则为模拟器(直到Apple发布Intel iOS设备)。

function isRunningInSimulator(device) {
    // Only valid after deviceReady
    return device && device.model.match(/x86/);
}

在最新版本的Device插件( https://www.npmjs.com/package/cordova-plugin-device )中,现在有一个isVirtual属性来表示此内容。

I don't know if it is possible to detect. 我不知道是否可以检测到。 One "workaround" is use a task manager (grunt, gulp, whatever) to copy configuration files from on folder to other. 一种“解决方法”是使用任务管理器(咕unt声,gulp等)将配置文件从文件夹复制到其他文件夹。 Then instead of running "cordova run android" (example) at the command line, you would run a task that copies the config file(s) from one specific folder (ie: development or production) an the application read the copied file. 然后,而不是在命令行上运行“ cordova run android”(示例),您将运行一个任务,该任务从一个特定的文件夹(例如,开发或生产)中复制配置文件,然后应用程序读取复制的文件。

You can take a look here http://devgirl.org/2013/11/12/three-hooks-your-cordovaphonegap-project-needs/ at the section "Replace Text Depending on Environment" 您可以在此处“根据环境替换文本”中查看http://devgirl.org/2013/11/12/three-hooks-your-cordovaphonegap-project-needs/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在真实设备上使用我的 Expo 应用程序? - How can I use my Expo App on a real device? 我如何知道我的promise.all是否并行运行? - How can I tell if my promise.all is running in parallel? 科尔多瓦:警报未在桌面环境中显示(它们在模拟器和真实设备上均能正常工作) - Cordova: Alerts not shown on desktop environment (They work fine on simulator and real device) 我可以判断我的Chrome扩展程序是否在Windows上运行吗? - Can I tell if my Chrome Extension is running on Windows? 如何在iPhone模拟器中查看嵌入式Web应用程序的启动日志记录 - How can I see the startup logging of an embedded web application in my iphone simulator 如何在Cordova应用程序中使用按钮? - How I can use Buttons in a cordova application? 如何实现需要持续运行的节点应用程序? - How can I implement my node application that needs to be constantly running? 如何在Cordova / Phonegap应用程序中从JavaScript调用Java函数? - How can I call a Java function from JavaScript in my Cordova/Phonegap application? 如何判断我的Javascript代码是否在Jint中运行? - How do I tell if my Javascript code is running in Jint? 如果在移动设备上查看脚本,如何禁用或隐藏脚本在我的网站上运行? - How can I disable or hide a script from running on my site if being viewed on a mobile device?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM