简体   繁体   中英

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". 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!

For iOS I just check the model - if matches /x86/ then it's the simulator (until Apple release an Intel iOS device).

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

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM