Xcode Simulator App Advanced

In this tutorial, you’ll learn about Xcode Simulator’s advanced features to improve your daily development experience. By Vidhur Voora.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Implementing Launch

In launch, replace #TODO with the following:

#1
BUNDLE_ID="$1"
LOCALE="$2"
LANGUAGE="$3"
CLONE_NAME="$LOCALE"

#2
SIMULATOR_ID=$(get_id_of_simulator)
if [ -z "$SIMULATOR_ID" ]; then
  echo "No device matching the name: $SIMULATOR_NAME"
  return
fi

echo "Simulator id: $SIMULATOR_ID"
echo "Bundle id: $BUNDLE_ID"
echo "Locale: $LOCALE"

#3
SIMULATOR_STATUS=$(get_status_of_simulator)
echo "Simulator Status: $SIMULATOR_STATUS"
if [ "$SIMULATOR_STATUS" = "Booted" ]; then
  echo "Making sure the device is shutdown first..."
  xcrun simctl shutdown "$SIMULATOR_NAME"
fi

#4
echo "Cloning the device with name $LOCALE..."
xcrun simctl clone "$SIMULATOR_NAME" "$CLONE_NAME"

#5
echo "Booting device..."
xcrun simctl boot "$SIMULATOR_NAME"
xcrun simctl boot "$CLONE_NAME"

#6
echo "Launching app..."
xcrun simctl launch "$SIMULATOR_NAME" "$BUNDLE_ID"

#7
xcrun simctl launch "$CLONE_NAME" "$BUNDLE_ID" -AppleLocale "$LOCALE" \
  -AppleLanguages "($LANGUAGE)"

Save the file. Here’s what this does, step by step. It:

  1. Gives additional parameters to launch, including the bundle ID of the application, the locale and the language to launch your app in. It declares a local variable CLONE_NAME, which is the name of the cloned device. In this case, the name of the cloned device is the locale you specify;
  2. Fetches the simulator’s ID by calling get_id_of_simulator. Then, it stores the result in SIMULATOR_ID. If there is no matching simulator, it exits the program;
  3. Fetches the simulator’s status by calling get_status_of_simulator. If the status is Booted, it runs the shutdown command to shut it down. To be cloned, a device must be shut down;
  4. Clones the simulator using the clone command;
  5. Boots both the original simulator and the cloned simulator using the boot command;
  6. Launches the app on the original simulator using the launch command;
  7. Launches the app using the launch command on the cloned simulator. It specifies the locale and language for the app to launch in.

Launching RayWonders in a Different Locale

Open Terminal, and run the following:

./sim_utility.sh launch Demo com.raywenderlich.RayWonders hi_IN hi

RayWonders launches in Hindi on a cloned simulator with the name hi_IN.

RayWonders in Hindi

No more going to the settings and switching the device language to test your app. You can now see your app in multiple languages simultaneously. Great job!

Note: If you’ve encountered issues running the script, make sure you have just one simulator with the name Demo. You can check the number of simulators available by running xcrun simctl list | grep Demo in the terminal. Delete any duplicate simulators and try again.

RayWonders also supports a few other languages. Run the following:

./sim_utility.sh launch Demo com.raywenderlich.RayWonders ja_Jp ja

RayWonders now launches in Japanese on a new simulator.

RayWonders in Japanese

As a fun challenge, implement two more commands in the sim_utility script.

  • Cleanup: This deletes a simulator given a simulator name.
  • Help: This prints a menu of all the commands supported by the script.

You’ll find the final version of the script with all these options implemented in the Scripts folder in the downloaded materials.

Where to Go From Here?

Download the project by clicking the Download Materials button at the top or bottom of this page.

In this tutorial, you’ve learned tons of useful simulator and command-line options. To learn more, please check out these WWDC videos:

I hope you’ve enjoyed this tutorial. If you have any questions or comments, please join the forum discussion below.