Skip to content

Instantly share code, notes, and snippets.

@AliSoftware
Last active August 4, 2023 00:13
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AliSoftware/b005809fa77f567dfe3af2ad41f79712 to your computer and use it in GitHub Desktop.
Save AliSoftware/b005809fa77f567dfe3af2ad41f79712 to your computer and use it in GitHub Desktop.
Script to install & launch an iOS build (for iphonesimulator platform) into an iOS Simulator. Especially useful for Fabric needing a first launch to upload
#!/bin/bash
#->Usage: launch_ipa.sh <PATH.app> [DEVICE_TYPE] [IOS_RUNTIME]
#->
#-> Create a new simulator, install the app in it, launch it, wait 20s then kill it.
#-> The main purpose of this script is to just launch an app and let the applicationDidFinishLaunching… callback be called
#->
#-> - [DEVICE_TYPE] the model of simulator to use. See `xcrun simctl list devicetypes` for the full list. Defaults to 'iPhone-6s'
#-> Note: You just need to provide the last part of the full device ID from
#-> com.apple.CoreSimulator.SimDeviceType.<DEVICE_TYPE>
#->
#-> - [IOS_RUNTIME] provides the OS version. See `xcrun simctl list runtimes` for the full list. Defaults to 'iOS-10-3'
#-> Note: You just need to provide the last part of the full runtim ID from
#-> com.apple.CoreSimulator.SimRuntime.<IOS_RUNTIME>
#
# It's especially useful for people using Fabric to distribute their iOS apps OTA,
# as Fabric requires the app to be launched at least once to create the app entry on fabric.io
# before being able to upload the ipa to their server.
# This script can be used on your CI machines to make that launch-at-least-once happen when necessary.
# (especially the very first time you build the app with that given BundleID)
#
# Author: O.Halligon, Aug. 2017
#
APP_PATH=$1
DEVICE_TYPE=${2:-"iPhone-6s"}
RUNTIME=${3:-"iOS-10-3"}
GRACE_TIME=20 # Number of seconds to give for the simulator to finish booting and launch the app
## Some input checks ##
if [ -z "$1" ]; then
sed -nE "s/^#->//p" "$0"; exit 1 # Prints #-> prefixed lines as a "usage" help then exit
fi
if [ ! -d "$APP_PATH" ] || [ ${APP_PATH##*.} != "app" ]; then
echo "You should provide a path to a '.app' bundle."
echo
sed -nE "s/^#->//p" "$0"; exit 1 # Prints #-> prefixed lines as a "usage" help then exit
fi
if [ ! $(defaults read "$APP_PATH"/Info CFBundleSupportedPlatforms | grep iPhoneSimulator) ]; then
echo "Provided app does not seem to support iPhoneSimulator"
exit 2
fi
## Extract data ##
BUNDLEID=$(defaults read "$APP_PATH"/Info CFBundleIdentifier)
SIM_NAME=AutomatedAppLauncher-$BUNDLEID-$RANDOM
## Main ##
# Create & launch simulator, install & launch app, wait, and cleanup
echo "- Creating new simulator $SIM_NAME ($DEVICE_TYPE, $RUNTIME)"
simuuid=$(xcrun simctl create "$SIM_NAME" "com.apple.CoreSimulator.SimDeviceType.$DEVICE_TYPE" "com.apple.CoreSimulator.SimRuntime.$RUNTIME") || exit $?
echo "- Launching simulator: $simuuid"
# Note: "xcrun simctl boot $simuuid" won't always launch the simulator UI. Using instruments instead as a workaround
xcrun instruments -w "$simuuid" 2>/dev/null || true # the command will always fail, but we just use it as a trick to force-launch the simulator UI anyway
echo "- Installing app on simulator..."
xcrun simctl install "$simuuid" "$APP_PATH" || exit $?
echo "- Launching app in simulator..."
xcrun simctl launch "$simuuid" "$BUNDLEID" || exit $?
echo "- Waiting ${GRACE_TIME}s to let it boot the new simulator, launch the app and call Fabric"
for i in $(seq 1 $GRACE_TIME); do echo -n '.'; sleep 1; done; echo " OK."
echo "- Killing app..." # This is not strictly necessary but avoids the Simulator UIWindow to still display the app UI even after the sim being deleted
xcrun simctl terminate "$simuuid" "$BUNDLEID" || exit $?
sleep 1
echo "- Uninstalling app..." # This is not strictly necessary but avoids the Simulator UIWindow to still display the app UI even after the sim being deleted
xcrun simctl uninstall "$simuuid" "$BUNDLEID" || exit $?
sleep 1
echo "- Shutting simulator down..."
xcrun simctl shutdown "$simuuid" || exit $?
echo "- Deleting simulator..."
xcrun simctl delete "$simuuid" || exit $?
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment