'How to dynamically get the UID of the IOS simulator device and then install in that device

How to dynamically get the UID of the IOS simulator device and then install it in that device. I currently have this BASH script which does the job, however, when I run that code on a different machine, I need to manually change the ID every time which makes the automation difficult. How can I get the UID just by defining the device type such as IPHONE.8 get the ID and use that instead

#!/bin/bash
export PATH=/usr/local/bin:$PATH

# define variables
DESTINATION="platform=iOS Simulator,name=iPhone 8,OS=13.4.1"
IPHONE_VERSION_ONE="iPhone-7"
APPDIR="$HOME/Library/Developer/Xcode/DerivedData/"
APP_NAME="In-House-iphonesimulator/test.app"
PHONE_ID_ONE="AB877AA-2178-4C29-BF4F-556456C"


xcrun simctl install $PHONE_ID_ONE $APP_LOCATION || { echo 'Unable to install App to Iphone7' ; exit 1; }

Also i put the below answer in the variable, however how to put quotes when store it in a variable?

PHONE_ID="instruments -s devices | grep -m 1 "iPhone 8" | awk -F'[][]' '{print $2}'"


Solution 1:[1]

instruments -s devices | grep "iPhone 8" | awk -F'[][]' '{print $2}'

Here what this command is doing:

  1. Listing all of your iOS devices
  2. Searching for iPhone 8 over the results
  3. Splitting the string by square brackets and printing the second token.

So you as a result should get UDID only: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX

If you have more than one iPhone 8 simulator in your system, you can limit the output by adding -m 1 key to the grep command:

instruments -s devices | grep -m 1 "iPhone 8" | awk -F'[][]' '{print $2}'

Just assign the result of this command to your PHONE_ID_ONE variable using $():

PHONE_ID="$(instruments -s devices | grep -m 1 'iPhone 8' | awk -F'[][]' '{print $2}')"

Hope this helps

Solution 2:[2]

With xctrace for example

   xctrace list  devices | grep -m 1 "iPhone 12" | awk '{print substr($0,length($0)-36,36)}'

Solution 3:[3]

You can get the list of simulators together with their ID :

instruments -s devices

Solution 4:[4]

I have used below command to get the device id of launched Simulator.

xcrun simctl list devices | grep "(Booted)" | grep -E -o -i "([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})"

We can get the id of specified device by given device name if there are multiple devices are launched

xcrun simctl list devices | grep "(Booted)" | grep "iPhone 12" |grep -E -o -i "([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Philippe
Solution 4 Deolas