Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Tell if a Cordova Application is Running in the Simulator

DZone's Guide to

How to Tell if a Cordova Application is Running in the Simulator

A simple plugin is all you need to know more about what's going on in your app simulator.

· Mobile Zone
Free Resource

Get gorgeous, multi-touch charts for your iOS application with just a few lines of code.

Just a quick note here but the most recent plugins release included a cool little update to the Device plugin. If you’ve never used it before, the plugin provides basic information about the app’s current working environment, including operating system and device model. In the most recent version, a new property was added:isVirtual.

As you can probably guess, this property will tell you if you’re running on a simulator or a real device. Now while I wouldn’t recommend shipping code that uses this normally, during testing it could be real useful. As an example, here is code that simply toggles what kind of camera should be used – the device camera or the photo gallery:

var sourceType = device.isVirtual ? Camera.PictureSourceType.PHOTOLIBRARY:Camera.PictureSourceType.CAMERA;

navigator.camera.getPicture(picDone, picFail, {
sourceType: sourceType,
destinationType:Camera.DestinationType.FILE_URI
});

Not rocket science, but useful. Just to be complete, here is a screen shot of the same code running on my device and simulator.

shot2

And if you want, you can grab the source for this demo here: https://github.com/cfjedimaster/Cordova-Examples/tree/master/checkforsim.

For folks curious, running this on Genymotion actually shows that it is considered a simulator, not a real device, even though you run it from the command line like a real device. Surprising.

.Net developers: use Highcharts, the industry's leading interactive charting library, without writing a single line of JavaScript.

Topics:
cordova ,mobile ,ios

Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}