How to Run an Ionic/PhoneGap App on a Specific Device/iOS Simulator


Introduction

Testing your application on a variety of devices and iOS versions is an inevitable step while developing an app. This is even more important when you are working on a hybrid mobile app using HTML based layouts as Phonegap/Cordova tools provide.

The first that comes to mind is to use iOS Simulator UI and simply open the Hardware window to select one of the available iOS versions and devices (see image 1). However, this can be an error-prone process.

In this post, Cordova CLI and ios-sim command line utility will be used in order to check what device types and iOS versions are available to launch your app on them. Note that the ios-sim is supported only for XCode versions 6 since version 3.x.

image 1

What are the available devices?

First, you need to find out the available devices you have on your development machine. Using Cordova CLI, cd into your project’s directory and execute the following command:

$ ./platforms/ios/cordova/lib/list-emulator-images

You should see a list of all the simulator images on your machine like so:

Run your app on a specific device

Assuming you are in your project’s folder, If you simply want to run your app on a particular device, you can run the following cordova command specifying the target name:

$ cordova run ios --target=”iPhone-4s”

You may need to quit your iOS Simulator for the switch to take effect.

Alternatively, in case you want to run an ionic app you can execute the command:

$ ionic run ios --target=iPhone-4s

However, even though you see in the list of available images a particular target name, there is a chance that you will face an error like this:

This, simply, means that you are using a target name that is not in the array of the available ones. In more detail, in the run.js file under the platforms/ios/cordova/lib/ path, there is an array of the valid targets:

Say you want to launch your app on iPad Air 2. Obviously, this target name is not included in the array of the valid targets. Therefore, you can add it as follows:

Run your app on a specific iOS

Similarly, you can use Ionic’s command to launch an Ionic app on a particular device with a specific iOS version. But, first, you need to execute the following ios-sim tool’s command in any directory:

$ ios-sim showdevicetypes

ios-sim uses the showdevicetypes term to indicate both the device types and the iOS versions that are available. This command’s output will be something similar to this:

In this example, simulators for iOS 8.4 and 9.0(the latest) are installed.

For example, if you want to run an app on iPad-Retina with 8.4 iOS, you simply add the following in the array of the valid targets:

Now, you are able to run ionic’s command as follows:

$ ionic run ios --target=’iPad-Retina, 8.4'

The defined target is going to be used to launch iOS Simulator and set the devicetypeid in deployToSim function:

This is pretty much what you can do directly with the following ios-sim command:

$ ios-sim launch platforms/ios/build/emulator/Catalogue.app --devicetypeid “com.apple.CoreSimulator.SimDeviceType.iPad-Retina, 8.4”

In this command, you must specify the path to your app’s executable file and a SimDeviceType displayed by the ios-sim’s previous command. Note that in order to create an executable file of your app you will need to build it first.

Interesting in Ionic?

Check out our Ionic and Ionic 2 Starter Kits and Application Templates.

Enjoyed this Post?

Follow us on Twitter, or Google+ or even check our CodeCanyon Profile. You may also take a look at our website www.appseed.io

References

Ionic, Cordova, ios-sim, Iain McConchie for the cover photo.

Happy coding,
The AppSeed team

/Stavros Kounis, skounis.github.io