Last Updated: February 25, 2016
·
4.772K
· kronenthaler

Chose iOS Simulator via command line

iOS development tools are far from being command line friendly, making automation a very difficult task.
Opening an specific simulator could be a big hassle, so here it goes a recipe to open, compile, install and run a specific project via command line.

build-and-run.sh

#!/bin/bash
# fully close the simulator and open the desired one
killall 'iOS Simulator'
open -a 'iOS Simulator' --args -CurrentDeviceUDID $UDID

# build:
xcodebuild -project *.xcodeproj -scheme $SCHEME -sdk iphonesimulator build SYMROOT=Build

# wait until there is a device booted
count=`xcrun simctl list | grep Booted | wc -l | sed -e 's/ //g'`
while [ $count -lt 1 ]
do
    sleep 1
    count=`xcrun simctl list | grep Booted | wc -l | sed -e 's/ //g'`
done

# uninstall old / install new
xcrun simctl uninstall booted $PACKAGE_NAME
xcrun simctl install booted `readlink Build/**/$APP_NAME.app || echo $PWD/Build/*/$APP_NAME.app`

# run app:
xcrun simctl launch booted $PACKAGE_NAME

As you can see, we used some practical environment variables:
UDID: the device id you want to use. this can be found with xcrun simctl list
PACKAGENAME: the bundle ID of the final app
APP
NAME: the app name without .app extension
SCHEME: the scheme used to compile the project.

How to use:

cd path/to/your/project
bash build-and-run.sh UDID=XXXX PACKAGE_NAME=com.example.app SCHEME=app APP_NAME=app