Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable/disable “Show as run destination” for all simulators

Is it possible to toggle the “Show as run destination” flag for multiple iOS simulators instead of changing it one by one in the "Device and Simulators" window? Is there a command line with that purpose?

enter image description here

like image 988
ricardopereira Avatar asked Jun 11 '18 11:06

ricardopereira


People also ask

How do I select simulated device as destination in Xcode?

For iOS, tvOS, and watchOS apps, you can choose a simulated device, under [Platform] Simulators, from the run destination menu next to the scheme menu in the toolbar.

How do I change the simulator on my iPhone?

With the simulator running go to File > Open Simulator > iOS [current version] and select the desired device.

How do I emulate an iPhone using Xcode?

To run your app in Simulator, choose an iOS simulator—for example, iPhone 6 Plus, iPad Air, or iPhone 6 + Apple Watch - 38mm—from the Xcode scheme pop-up menu, and click Run. Xcode builds your project and then launches the most recent version of your app running in Simulator on your Mac screen, as shown in Figure 1-1.


1 Answers

I decided to find it by myself using fswatch. BTW, it's really useful for this kind of situations. By monitoring the changes of file while toggling the "Show as run destination" flag, I found out that Xcode was changing the ~/Library/Preferences/com.apple.dt.Xcode.plist file 🙌

After some analysis, I noticed the key that I needed to change to achieve what I had in mind. The key is DVTIgnoredDevices and it holds an array of simulators. So, each simulator UUID in that list will be ignored in Xcode.

Now, I can change the DVTIgnoredDevices key using defaults command line tool specifying the needed value type:

-array Allows the user to specify an array as the value for the given preference key:

defaults write somedomain preferenceKey -array element1 element2 element3

The specified array overwrites the value of the key if the key was present at the time of the write. If the key was not present, it is created with the new value.

Example:

defaults write com.apple.dt.Xcode DVTIgnoredDevices '(
  "80E16DBC-2FE5-48AC-8A44-1F5DEFA00EA7",
  "B8C4D5FF-8F1A-4895-BD16-CCAFECD71098"
)'

After setting the DVTIgnoredDevices key, you need to clean the DerivedData folder and restart Xcode. To clean the DerivedData folder, please see this answer or just run the shortcut shift+alt+cmd+k (that's what I usually do).

Tested on Xcode Version 9.4 (9F1027a).

UPDATE:

I usually like to have just a couple of Simulators in the list, so I decided to do a script using instruments -s devices and add all the current Simulators to the DVTIgnoredDevices key. Then I chose which simulator(s) will be shown 😊

Xcode-hide-all-iPhone-simulators.sh

simulatorsIdentifiers=$(instruments -s devices |
  grep -o "iPhone .* (.*) \[.*\]" | #only iPhone
  grep -o "\[.*\]" | #only UUID
  sed "s/^\[\(.*\)\]$/\1/" | #remove square brackets
  sed 's/^/"/;$!s/$/"/;$s/$/"/' | #add quotes
  sed '$!s/$/,/' #add comma to separate each element
)

arrayOfSimulatorsIdentifiers=($(echo "$simulatorsIdentifiers" | tr ',' '\n'))

# Add simulators to DVTIgnoredDevices
echo "${#arrayOfSimulatorsIdentifiers[@]}"
for index in "${!arrayOfSimulatorsIdentifiers[@]}"
do
    echo "$index Adding: ${arrayOfSimulatorsIdentifiers[index]}"
done

defaults write com.apple.dt.Xcode DVTIgnoredDevices -array ${arrayOfSimulatorsIdentifiers[@]}

Gist file

like image 178
ricardopereira Avatar answered Oct 15 '22 03:10

ricardopereira