Last Updated: February 25, 2016
· 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.

# 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 ]
    sleep 1
    count=`xcrun simctl list | grep Booted | wc -l | sed -e 's/ //g'`

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

# 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
NAME: the app name without .app extension
SCHEME: the scheme used to compile the project.

How to use:

cd path/to/your/project