stackErr
stackErr

Reputation: 4170

Script to install app in iOS Simulator

I am trying to automate the process of building an app, running unit tests, and finally running UI tests.

I am building the app via command line (xcodebuild -sdk iphonesimulator6.0) in some directory.

How do I install this app to the iOS simulator via command line (in ~/Library/Application Support/iPhone Simulator//Applications)?

I tried:

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone\ Simulator.app/Contents/MacOS/iPhone\ Simulator -SimulateApplication MyApp.app/MyApp

but this opens a new finder window named "iOS Simulator could not find the application to simulate".

Upvotes: 13

Views: 20025

Answers (2)

Palpatim
Palpatim

Reputation: 9262

As of Xcode 6, you should be able to use simctl to accomplish this.

1) Get the list of available devices:

xcrun simctl list devices

1a) Assuming you have jq installed, you can use it to get only those devices that are actually available:

xcrun simctl list devices -j \
| jq -rc '.[] | .[] | .[] | select( .availability | contains( "(available)" ) ) '

1b) Or even filter further by iPhone or iPad:

xcrun simctl list devices -j \
| jq -rc '.[] | .[] | .[] | select( .name | contains( "iPhone" ), contains( "iPad" ) ) | select( .availability | contains( "(available)" ) ) '

2) Once you have the UDID of the device you want to install to:

xcrun simctl install $DEVICE_UDID /path/to/your/app

2a) Or, if you want to just install to the booted device:

xcrun simctl install booted /path/to/your/app

Where this gets really handy is if you want to run the same app on all the devices:

1) Reset / erase all simulators:

xcrun simctl erase all

2) Open one Simulator instance for each test:

open -n /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app
(Ignore the 'Booted' error and switch hardware.)

3) Get UDIDs of available devices we want to install to:

DEVICES=$( xcrun simctl list devices -j | jq -rc '.[] | .[] | .[] | select( .name | contains( "iPhone" ), contains( "iPad" ) ) | select( .availability | contains( "(available)" ) ) | select( .state == "Booted" ) | .udid ' )

4) Install the app (which must be built for the appropriate simulator SDK):

for device in DEVICES ; do xcrun simctl install $device /path/to/app ; done

5) For convenience, launch the app on each device:

for device in $DEVICES ; do xcrun simctl launch $device your.product.app.id ; done

Upvotes: 22

stackErr
stackErr

Reputation: 4170

I made a shell script which installs the app to the simulator.

#!/bin/sh
# Pick a uuid for the app (or reuse existing one).
if ! [ -f installApp.uuid ]; then
    uuidgen > installApp.uuid
fi
UUID=$(cat installApp.uuid)
#create supporting folders
TOPDIR="$HOME/Library/Application Support/\
iPhone Simulator/6.0/Applications/$UUID/"
mkdir -p "$TOPDIR"
mkdir -p "$TOPDIR/Documents"
mkdir -p "$TOPDIR/Library"
mkdir -p "$TOPDIR/tmp"
mkdir -p "$TOPDIR/$1.app"

#copy all the app file to the simulators directory
cp -r * "$TOPDIR/$1.app"

How to use this script to install the app:

  1. Change this line:

    TOPDIR="$HOME/Library/Application Support/iPhone Simulator/6.0/Applications/$UUID/"
    

    to reflect the version of iPhone Simulator you are using i.e. 6.0/7.1.

  2. Save the script as installApp.sh in your project_name.app/ folder.

  3. Open a terminal window and run installApp from the project directory.

    • So if I have my project_name.app/ with a project inside. Type in the terminal:

      cd path/to/project_name.app/

    • Then ./installApp to install the app to the simulator.

I got the idea from Jeffrey Scofield: Run iOS Simulator from the Command Line


For iOS8 and the new XCode Apple changed a few things and made it difficult to install apps via the command line. It is still possible to do:

  1. First you need to find the app directory (assuming you have installed apps): find ~/Library/Developer/CoreSimulator/Devices -name '*.app'

  2. This will list all the paths that have a custom app installed. E.g.

/34792D41-55A9-40F5-AAC5-16F742F1F3E4/data/Containers/Bundle/Application/4BA2A285-6902-45A8-9445-FC3E46601F51/YourApp.app

  1. There will be multiple UUID parent directories with the structure above. Each UUID corresponds to a simulator for a different device. Open the top directory and you will find a device.plist file. This file will contain the device it is simulating:
<dict>
    ...
    <string>34792D41-55A9-40F5-AAC5-16F742F1F3E4</string>
    <string>com.apple.CoreSimulator.SimDeviceType.iPhone-6-Plus</string>
    ...
</dict>
  1. If you want to install your app for iPhone-6-Plus this is the directory you would do it in. Run the shell script above to install the app. Change the TOPDIR path to $HOME/Library/Developer/CoreSimulator/Devices/{UUID for device}/data/Containers/Bundle/Applications/$UUID/

Upvotes: 19

Related Questions