How to tell if a Cordova application is running in the simulator

This post is more than 2 years old.

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.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Nic Raboy posted on 12/3/2015 at 4:23 AM

Do you know if there is a way to determine if the app is being run in a web browser?

Comment 2 (In reply to #1) by Raymond Camden posted on 12/3/2015 at 12:28 PM

Heh well that depends. Are you talking about:

Just running www via http.
Running via the browser platform.
Running via cordova serve. (Or ionic serve.)

Comment 3 (In reply to #1) by Raymond Camden posted on 12/3/2015 at 12:32 PM

So "run as browser" - the device plugin reports platform is browser. That's easy.

For "run via http" and ionic serve, the device plugin doesn't exist at all.

You could check for window.cordova - it won't exist.

Comment 4 (In reply to #3) by Nic Raboy posted on 12/3/2015 at 3:57 PM

All of the above is what I was looking for. ng-cordova-oauth is not compatible in a browser and I want to make sure no one tries to use such.

Eliminates a lot of wasted time trying to troubleshoot someones problem when the result is they are using a service that is clearly outlined in the README as not compatible.

Comment 5 (In reply to #4) by Raymond Camden posted on 12/3/2015 at 4:08 PM

I'd check for window.cordova.

Comment 6 (In reply to #5) by Nic Raboy posted on 12/3/2015 at 4:10 PM

I'd have to re-evaluate, but I think window.cordova wasn't enough when it came to ionic serve.

I'll get back to you.

Comment 7 (In reply to #6) by Raymond Camden posted on 12/3/2015 at 4:15 PM

don't allow if: window.cordova is null OR device.platform is browser.

Comment 8 (In reply to #7) by Nic Raboy posted on 12/3/2015 at 4:32 PM

Will give it a shot. Thanks!