How to Run an Ionic/Cordova App on 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 and Cordova tools.

The first that comes to mind is that you use the iOS Simulator 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 supports Xcode 8 and greater only since version 5.x.

What are the available devices?

First, you need to find out the available devices you have on your development machine. Using Cordova CLI, from your project’s directory, 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 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-5s

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 cordova run ios --target=iPhone-5s

Run your app on a specific iOS

Similarly, you can use Ionic CLI’s command to launch an Ionic app on a particular device with a specific iOS version. To do this, firstly, 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. The output of this command will be something similar to this:

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

$ ionic cordova run ios --target='iPhone-5s, 11.0'

The defined target is going to be used to launch the iOS Simulator and set the devicetypeid in deployToSim function. The function can be located in your project folder, under /platforms/ios/cordova/lib/run.js

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

$ ios-sim launch platforms/ios/build/emulator/TestApp.app --devicetypeid 'com.apple.CoreSimulator.SimDeviceType.iPhone-5s, 11.0'


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

Deploy your app from Xcode

From Xcode, open the TestApp.xcworkspace file under the platforms/ios path. TestApp is the name of our test application. Make sure to substitute it with the name of your own application.

Or use the “open” command from your terminal:

$ open platforms/ios/TestApp.xcodeproj


Select the preferred device, for example iPhone 8 Plus.

Press the Run button.

This button builds, deploys, and runs the application in the simulator. The iOS simulator opens with the app running.

References

Ionic docs, iOS Cordova Platform Guide, iOS-sim, Pexels for the cover photo

Stay tuned

Join our list and sing up to get early access to our latest news and free tutorials about Ionic.