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!
Archived Comments
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!
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).
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 ?
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. :)
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
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.
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.
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.
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.
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. :)
That's fine, just include a link back.
Sure, I will do that. Thanks Ray. I will send you the link later.
Hey Ray, I posted on the blog I told you before and put the link for your page. Thanks :)
I forgot, here is the link for the post: http://www.phonegapbrasil.c...
In the latest cordova ios version this has moved one dir further:
projectroot/platforms/ios/cordova/lib/run.js #line 443 to 45 ;)
Thanks!