Skip to content

Instantly share code, notes, and snippets.

@TetsuOtter
Last active August 23, 2023 15:15
Show Gist options
  • Save TetsuOtter/15f18a1bd4b0212a7bca205f0bdccd00 to your computer and use it in GitHub Desktop.
Save TetsuOtter/15f18a1bd4b0212a7bca205f0bdccd00 to your computer and use it in GitHub Desktop.
launch iOS App on Device and Simulator from terminal
MIT License
Copyright (c) 2023 Tetsu Otter
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
#!/bin/bash
cd `dirname $0`
TARGET_PROJ="MauiProject/MauiProject.csproj"
TARGET_FRAMEWORK="net8.0-ios"
UDID_PATTERN="[[:alnum:]]{8}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{4}-[[:alnum:]]{12}"
ARGS=($@)
SCRIPT_NAME=`basename $0`
if [ "$1" = "-h" ]; then
cat << END
iOS Simulator Launcher
Usage: $SCRIPT_NAME [UDID/DeviceName] [dotnet command options]
If you don't specify UDID/DeviceName, you can select Device from available device list.
dotnet command will executed like below ...
dotnet build -t:Run $TARGET_PROJ -f $TARGET_FRAMEWORK -r iossimulator-x64 --no-self-contained --nologo "/p:_DeviceName=[DeviceName|:v2:udid=UDID]" [dotnet command options]
---
Exec Example: Run Release build on the simulator (UDID=12345678-1234-1234-1234-123456789ABC)
$SCRIPT_NAME 12345678-1234-1234-1234-123456789ABC -c Release
Exec Example: Run Release build on the Device (DeviceName=Sample_iPad)
$SCRIPT_NAME Sample_iPad -c Release
END
exit 0
fi
if [ -z "$1" ] || [[ $1 = -* ]]; then
COUNTER=0
declare -a DeviceArr=()
while read ListLine
do
if [ -z "$ListLine" ] || [[ "$ListLine" = ==*== ]]; then
echo "$ListLine"
continue
fi
_UDID=`echo "$ListLine" | grep -oE "$UDID_PATTERN"`
if [ ! -z "$_UDID" ]; then
echo "[$COUNTER]" "$ListLine"
DeviceArr[$COUNTER]=":v2:udid=$_UDID"
else
echo "[$COUNTER]" "$ListLine"
UnneededPart=`echo "$ListLine" | grep -ioE ' \([0-9\.]+\) \([0-9a-f\-]+\)$'`
DeviceArr[$COUNTER]=${ListLine%$UnneededPart}
fi
COUNTER=`expr $COUNTER + 1`
done <<< "$(xcrun xctrace list devices)"
ARRLEN=${#DeviceArr[*]}
if [ $ARRLEN -eq 0 ]; then
echo "Error: No Device detected." 1>&2
exit 0
fi
echo "Please select the device you want to launch App on."
INPUT_MAX=`expr $ARRLEN - 1`; read -p "0 ~ $INPUT_MAX [0]: " answer
if [ -z "$answer" ]; then
answer=0
fi
IS_NUM_PATTERN='^[0-9]+$'
if ! [[ $answer =~ $IS_NUM_PATTERN ]] || [ ! \( 0 -le $answer -a $answer -lt $ARRLEN \) ]; then
echo "Invalid Selection ($answer)" 1>&2
exit 1
fi
DeviceName="${DeviceArr[$answer]}"
else
ARG_UDID=`echo "$1" | grep -oE "$UDID_PATTERN"`
if [ -z "$ARG_UDID" ]; then
DeviceName=$1
else
DeviceName=":v2:udid=$ARG_UDID"
fi
ARGS[0]=""
fi
UDID=`echo "$DeviceName" | grep -oE "$UDID_PATTERN"`
if [ -z "$UDID" ]; then
echo "... DeviceName: $DeviceName (Run on Physical Device)"
RUNTIME_IDENTIFIER='ios-arm64'
else
echo "... UDID: $UDID (Run on Simulator)"
RUNTIME_IDENTIFIER='iossimulator-x64'
fi
if [ ! -z "$DeviceName" ]; then
dotnet build -t:Run $TARGET_PROJ -f $TARGET_FRAMEWORK -r $RUNTIME_IDENTIFIER --self-contained --nologo "/p:_DeviceName=$DeviceName" ${ARGS[@]}
fi
== Devices ==
[0] SampleHostMac (XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX)
[1] SampleRealDevice (16.6) (XXXXXXXX-XXXXXXXXXXXXXXXX)
== Simulators ==
[2] iPad (10th generation) Simulator (16.4) (XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX)
[3] iPad Air (5th generation) Simulator (16.4) (XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX)
[4] iPad Pro (11-inch) (4th generation) Simulator (16.4) (XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX)
[5] iPad Pro (12.9-inch) (6th generation) Simulator (16.4) (XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX)
[6] iPad mini (6th generation) Simulator (16.4) (XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX)
[7] iPhone 14 Simulator (16.4) (XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX)
[8] iPhone 14 Plus Simulator (16.4) (XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX)
[9] iPhone 14 Pro Simulator (16.4) (XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX)
[10] iPhone 14 Pro Max Simulator (16.4) (XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX)
[11] iPhone SE (3rd generation) Simulator (16.4) (XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX)
Please select the device you want to launch App on.
0 ~ 11 [0]: 6
... UDID: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX (Run on Simulator)
Determining projects to restore...
# launch App on iOS device named `Real_iOS_Device`
./iosSim.sh Real_iOS_Device
# launch debug build App on iOS device named `Real iOS Device`
./iosSim.sh 'Real iOS Device' -c Debug
# launch App on Simulator Device that has UUID: 12345678-1234-1234-1234-123456789ABC
./iosSim.sh 12345678-1234-1234-1234-123456789ABC
# select device from list and launch on selected device
./iosSim.sh
# then, device list (also real device and simulator device) is printed
# select the device index and launch on it
# see usage.select_sample.log
# format: [index] DEVICE_NAME (OS Version) (UDID for Real Device / UUID for Simulator Device)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment