Twitter: raymondcamden


Address: Lafayette, LA, USA

Targeting a device type with Cordova Emulate

07-21-2014 6,849 views Mobile 14 Comments

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!

14 Comments

These comments will soon be imported into Disqus. To add a comment, use Disqus above.
  • Michal Mocny #
    Commented on 07-22-2014 at 10:43 AM
    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!
  • Commented on 07-22-2014 at 11:53 AM
    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).
  • Commented on 07-23-2014 at 5:20 AM
    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 ?
  • Commented on 07-23-2014 at 7:34 AM
    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. :)
  • Commented on 07-23-2014 at 8:41 AM
    Thank you Raymond,

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

    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
  • Commented on 07-23-2014 at 10:50 AM
    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.
  • Commented on 07-23-2014 at 12: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.
  • Commented on 07-23-2014 at 12: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.
  • Commented on 07-23-2014 at 12: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.
  • Commented on 07-27-2014 at 8:07 PM
    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. :)
  • Commented on 07-27-2014 at 9:08 PM
    That's fine, just include a link back.
  • Commented on 07-28-2014 at 12:45 PM
    Sure, I will do that. Thanks Ray. I will send you the link later.
  • Commented on 07-29-2014 at 10:27 PM
    Hey Ray, I posted on the blog I told you before and put the link for your page. Thanks :)
  • Commented on 07-29-2014 at 10:28 PM
    I forgot, here is the link for the post: http://www.phonegapbrasil.com.br/escolhendo-target...