Change Language Of All iOS Simulators - ios

We have the issue that with every new Xcode version all of our simulators are re-created in with system language German and system region Germany. But we need to work with a special "development language". In our case that is en_BZ.
Additionally when I try to set the language now wirth Xcode 14 the simulator crashes and the only solution to bring it back is to erase its data.
How can I change system language/region permanently so I don't have to set that tediously with every new Xcode version and for every simulator device?

This is actually possible suing the tool simctl. Using the following set of commands this can be fully automated:
xcrun simctl list -j "devices" | jq -r '.devices | map(.[])[].udid' | parallel 'xcrun simctl boot {}; xcrun simctl spawn {} defaults write "Apple Global Domain" AppleLanguages -array en; xcrun simctl spawn {} defaults write "Apple Global Domain" AppleLocale -string en_BZ; xcrun simctl shutdown {}'
Explanation
The {} is a placeholder provided by parallel and it represents the respective udid obtained in the first steps above. This is what the single commands do, one by one:
xcrun simctl list -j "devices": Lists all devices that are also available in Xcode in JSON format.
jq -r '.devices | map(.[])[].udid': Filters the udid of each device from the JSON output. It uses the tool jq which is a super powerful JSON parser. You have to install that using brew install jq.
parallel [...]: This launches the following set of command instructions in parallel. This is super useful since the first one, booting all simulators takes a lot of time. Doing this one by one would take forever. Please brew install parallel first.
xcrun simctl boot {}: Boots each simulator.
xcrun simctl spawn {} defaults write "Apple Global Domain" AppleLanguages -array en: Sets English as one pf the preferred languages.
xcrun simctl spawn {} defaults write "Apple Global Domain" AppleLocale -string en_BZ: Sets English as used system language and Belize as system region.
xcrun simctl shutdown {}: Shutdown each simulator again.

Related

Add Language in Xcode 14.1 Simulator problem

I updated my Xcode to 14.1. but when I want to add a language in Setting, it stays on this page and I can't do any thing.
All I can do is Erase All content and Setting to return to normal state. It works on my MacBook Air M1, but no on my MacBook Pro 2019 Intel i7
It works on M1/M2 but it doesn't on Intel. You can use the command line tool simctl as a workaround.
To set the preferred language to e.g. "German" use the following command:
xcrun simctl spawn <UUID> defaults write "Apple Global Domain" AppleLanguages -array de
To change the region use:
xcrun simctl spawn <UUID> defaults write "Apple Global Domain" AppleLocale -string 'de_DE'
Replace <UUID> with the UUID of the device you want to change. To get the currently booted device(s) use:
xcrun simctl list 'devices' 'booted'
After you have changed language and/or region you need to shutdown and reboot the device like this.
xcrun simctl shutdown <UUID> followed by xcrun simctl boot <UUID>
If you only want to change the currently booted device(s) you can just use 'booted' instead of the UUID. Like so:
xcrun simctl shutdown 'booted'
So, to change the preferred language of the currently booted device(s) to English, just use:
xcrun simctl spawn 'booted' defaults write "Apple Global Domain" AppleLanguages -array en
To change language and/or region the desired device(s) must be booted.

How do I erase an Xcode 10 simulator clone through CLI?

Do they share the same UDID?
How are they implemented under the hood?
The scenario is I have 4 clones running UI tests in parallel. I need a clean simulator for some tests (but want to keep random test order)
Command to erase all testing simulators (you'll need to restart Xcode after that):
xcrun simctl --set testing delete all
Overall, it should be better to just reset those sims (you won't need to restart after that):
xcrun simctl --set testing shutdown all
xcrun simctl --set testing erase all
To erase specific one you first need to get his ID with list devices and then shutdown and erase:
xcrun simctl --set testing list devices
xcrun simctl --set testing shutdown 2BC2B50E-C4BA-45B9-9C73-AF5097BA1F0B
xcrun simctl --set testing erase 2BC2B50E-C4BA-45B9-9C73-AF5097BA1F0B
Thanks Scott McCoy for his answer.
I have not been able to find any interface with cloned simulators via simctl. You can completely remove them by deleting their folder under ~/Library/Developer/XCTestDevices. They each get their own UUID, but again, they don't get listed in simctl as far as I can tell.
You can list clones like this:
xcrun simctl --set testing list
But any time I try to use device IDs with xcrun commands they fail with Invalid device: <device id>
On your status bar with Xcode open on your mac go to Windows->Devices and Simulators. There you will see a list of all the available simulators and you can add new or delete existing from there.
you can reset all the simulators by:
xcrun simctl erase all
to reset a particular simulator use:
xcrun simctl erase your_simulator_id

How can I tell when the iOS simulator has booted to its home screen?

I use open -n /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app/ --args -CurrentDeviceUDID xxxxx to start a simulator alongside any number of others, and I can poll xcrun simctl list | grep xxxxx to find out when it's started its boot process.
How can I determine that the simulator has finished its boot-up process and is idling at its home screen?
Currently I'm polling ~/Library/Logs/iOS Simulator/{version}/system.log until it's quiet for a few seconds, but that's kinda lame.
Is there something nicer, like an xcrun simctl getenv?
You could poll the com.apple.springboard.services mach service for checkin. Eg:
~ $ simctl spawn booted launchctl print system | grep com.apple.springboard.services
0x1c407 M D com.apple.springboard.services
...
~ $ simctl spawn booted launchctl print system | grep com.apple.springboard.services
0x1c407 M A com.apple.springboard.services
edit:
With recent builds, you can also run xcrun simctl bootstatus <UDID> to monitor this progress. That's likely a much more elegant solution than polling launchctl these days.
edit (again):
In addition to using 'xcrun simctl bootstatus ' to monitor the progress, you can use it to kick off the boot if it hasn't started already. Check xcrun simctl help bootstatus for more info.

How to launch iOS Simulator in specific language from command line?

I need to lauch iOS Simulator that uses specific language using command line. So I found that I can use
instruments -w <device>
and it is working great, I can set specific device. But how can I run simulator with specific language? I've tried adding
-AppleLanguages -AppleLocale
but there are some warnings:
Instruments Usage Error : Specified target process is invalid: -AppleLanguage
thanks!
The only way to launch iOS Simulator with specific language is to change contents of its .GlobalPreferences.plist file. Using xcrun tool will not work because it passess arguments to launched app and not changing language of simulator itself. Manipulation on .GlobalPreferences.plist is quite difficult because it is a binary plist file, so you cannot modify it as 'normal' xml. The easiest way to change its contents is to write simple Xcode Command Line Tool application, Foundation SDK has all tools needed to modify binary plists.
To run your app must be installed and located (if not, will open default language)
Use this command to run your app with some language
xcrun simctl launch <deviceid> <appid> -AppleLanguages "(pt-BR)"
Sample:
xcodebuild -sdk iphonesimulator8.4 -arch i386 install DSTROOT=SomeFolder
xcrun instruments -w "iPhone 6 (8.4 Simulator)"
xcrun simctl install booted SomeFolder/Applications/YourApp.app
xcrun simctl launch booted com.yourdomain.yourapp -AppleLanguages "(pt-BR)"
Have a look at:
https://developer.apple.com/library/ios/documentation/MacOSX/Conceptual/BPInternational/TestingYourInternationalApp/TestingYourInternationalApp.html
Search for "Testing Specific Languages and Regions"
Perhaps it could also be a solution creating different targets. Each target has configured another language

How do I remove app from iOS 8 Simulator from command line?

I have an automation running in the iOS Simulator that I have to remove before another run. How do I remove the app from the iOS Simulator from the command line?
For each simulator device directory (located at ~/Library/Developer/CoreSimulator/Devices/*), I
tried to delete ./data/Containers/Bundle/Application/ and ./data/Containers/Data/Application/.
Even when I tried to delete the app by long pressing the app in the Simulator (the app becomes jiggly) and click on the X button, the user defaults were not being cleared. I want the app state to be 100% clean.
I found a good solution to solve this problem.
With Xcode 6.1, to uninstall an app, use the following command:
xcrun simctl uninstall booted com.example.apple-samplecode.UICatalog
where com.example.apple-samplecode.UICatalog is the bundle identifier of the application you wish to uninstall.
One approach that we found for deleting user defaults is to delete all files in the ./data/Library/Preferences/* in addition to deleting application and data directories.
However, in Xcode 6, the command xcrun has new subcommand called simctl that allows me to manage iOS Simulator including resetting the simulator, and installing the application.
The solution that I came up with is to use the command
xcrun simctl erase [device ID]
Example
If xcrun simctl list(†) returns
9DDA0CFE-7CEC-40B6-A343-1EC01F282B22 (active, disconnected)
Watch: Apple Watch Series 2 - 42mm (88474523-163E-4021-B591-2AECBFA26997) (Shutdown)
Phone: iPhone 7 Plus (5785E680-15CD-42D3-82AB-597286A270C5) (Shutdown)
then run these 2 commands
xcrun simctl erase 88474523-163E-4021-B591-2AECBFA26997
xcrun simctl erase 5785E680-15CD-42D3-82AB-597286A270C5
(†) The device ID can be obtained from running
xcrun simctl list
This will reset the simulator (equivalent to iOS Simulator > Reset Contents and Settings... menu item).
With Xcode 6.0.1 (Build 6A317), there is either a bug or a change in behavior where when you uninstall an application, user defaults are not removed.
Usage: simctl [--noxpc] [--set <set path>] <subcommand> ... | help [subcommand]
Command line utility to control the iOS Simulator
For subcommands that require a <device> argument, you may specify a device UDID
or the special "booted" string which will cause simctl to pick a booted device.
If multiple devices are booted when the "booted" device is selected, simctl
will choose one of them.
Subcommands:
create Create a new device.
delete Delete a device.
erase Erase a device's contents and settings.
boot Boot a device.
shutdown Shutdown a device.
rename Rename a device.
getenv Print an environment variable from a running device.
openurl Open a URL in a device.
addphoto Add a photo to the photo library of a device.
install Install an app on a device.
uninstall Uninstall an app from a device.
launch Launch an application by identifier on a device.
spawn Spawn a process on a device.
list List available devices, device types, or runtimes.
notify_post Post a darwin notification on a device.
icloud_sync Trigger iCloud sync on a device.
help Prints the usage for a given subcommand.
Reset all Content & Settings in a single command
Quit iPhone Simulator
In Terminal, run:
xcrun simctl erase all
This will reset content and settings of all the simulators for the active version of Xcode (the one referenced by xcode-select -p).
xcrun simctl uninstall simulatorIdentifier appBundleId

Resources