Targeting a Device Type with Cordova Emulate
Join the DZone community and get the full member experience.Join For Free
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!
Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.