Targeting a device type with Cordova Emulate

This post is more than 2 years old.

Today I've got yet another Cordova tip that was probably known to most people. I'm working on a sample application that needs to run on an iPad, not an iPhone, but the problem was that every time I ran cordova emulate ios, it would fire up the application in an iPhone. If I switched the hardware in the iOS simulator settings it would work, but when I recompiled and reran, it reverted to an iPhone. Turns out, there is a simple solution for this.

I had never noticed this, but cordova emulate is simply an alias for cordova run --emulator. If you run cordova at the command line with no arguments, you will see a few options for the run command. These options include device and target. If you check the docs for the CLI, it doesn't specify what these do. To be honest, I was a bit confused at first.

The --device flag is simply a way to say that you want to run the code on a device. Which is what cordova run means anyway, but the flag is there (as far as I can tell) for completeness sake to be the corollary to the --emulator flag.

The --target flag is the interesting one. This too isn't documented, but if you open up projectroot/platforms/ios/cordova/run, it is documented in the shell script.


# Valid values for "--target" (case insensitive):
#     "iPhone (Retina 3.5-inch)" (default)
#     "iPhone (Retina 4-inch)"
#     "iPhone"
#     "iPad"
#     "iPad (Retina)"

Pretty simple, right? So for my testing, the solution was simple: cordova emulate ios --target="iPad". Worked like a charm. I also looked into the Android folder and while there wasn't a simple list, from what I can see you can pass the name of an AVD. So for example, I've got an AVD called KitKat, so I was able to do: cordova emulate android --target=KitKat and it fired up the bits in that particular emulator. (Although I'm sure as hell trying to avoid the emulator now that I've got Genymotion working well.)

p.s. Thanks to devgeeks and shazron in IRC for helping me with this post!

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 Michal Mocny posted on 7/22/2014 at 7:43 PM

One tip: I use --device flag to say "run this only on device, and do *not* start emulator as fallback automatically". This way, I get a cordova launch error early on if device isn't properly attached / connected.

Launching the android emulator from scratch can take a while, so its otherwise a costly mistake to make!

Comment 2 by Raymond Camden posted on 7/22/2014 at 8:53 PM

Oh nice, yeah, I can see that being a good idea.

Checkout Genymotion. I plan on never running the Android emulator again (except for the times I accidentally open it).

Comment 3 by Gregory Besson posted on 7/23/2014 at 2:20 PM

Dear Raymond,

Thanks for the tip. What would have been great is the possibility to also choose the orientation of the device when I emulate.

Any clue ?

Comment 4 by Raymond Camden posted on 7/23/2014 at 4:34 PM

I don't believe so. I checked ios-sim, the CLI used by Cordova when it does emulation, and I don't see an option there. One may exist for Android. (But again, don't emulate Android. :)

Comment 5 by Gregory Besson posted on 7/23/2014 at 5:41 PM

Thank you Raymond,

We've had the exact same conclusion. This is why we've created this project : Cordova Fuse (https://github.com/AdFabCon....

It's based mainly on great work from Cordova fans and we've gathered the tips and ideas into this Fuse.
One of its features is the capability of dynamically change this variable for iOS during dev.

If you find time to review it, let me know if you think it could be interseting to go on this way.

Kind regards,
Greg

Comment 6 by Raymond Camden posted on 7/23/2014 at 7:50 PM

I'm still reading, but I have to be honest. After the "intro" in the readme, I have *no* idea what your project does. Like - seriously - it makes no sense.

Comment 7 by gregory besson posted on 7/23/2014 at 9:51 PM

Thanks for your answer Raymond,

We'll try to be more descriptive in the readme. In 1 short sentence, this project aims to gather interesting hooks into one repository and make them work the same way (based on the same configuration file).

Anyway, it's maybe not that interesting... Sorry for that.

Comment 8 by Raymond Camden posted on 7/23/2014 at 9:53 PM

No no, that *is* interesting, and a good idea I think. Your docs just need some improvement I guess. ;) Please keep me informed on this project as you make progress.

Comment 9 by Gregory Besson posted on 7/23/2014 at 9:59 PM

I have been afraid we were not on the right way.
I must admit that our doc is not very relevant. I'll review it.
And I stop polluting your blog and will keep you informed of our progress.
Thank you very much for your time Raymond.

Comment 10 by Matheus Rocha posted on 7/28/2014 at 5:07 AM

Nice tip Raymond, if you allow me I want to publish a post about this too in portuguese for the readers of phonegap brazilian community. :)

Comment 11 by Raymond Camden posted on 7/28/2014 at 6:08 AM

That's fine, just include a link back.

Comment 12 by Matheus Rocha posted on 7/28/2014 at 9:45 PM

Sure, I will do that. Thanks Ray. I will send you the link later.

Comment 13 by Matheus Rocha posted on 7/30/2014 at 7:27 AM

Hey Ray, I posted on the blog I told you before and put the link for your page. Thanks :)

Comment 14 by Matheus Rocha posted on 7/30/2014 at 7:28 AM

I forgot, here is the link for the post: http://www.phonegapbrasil.c...

Comment 15 by Edwin Klesman posted on 9/26/2016 at 12:28 PM

In the latest cordova ios version this has moved one dir further:

projectroot/platforms/ios/cordova/lib/run.js #line 443 to 45 ;)

Comment 16 (In reply to #15) by Raymond Camden posted on 9/26/2016 at 1:14 PM

Thanks!