[英]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.