Search code examples
iosxcodeios-simulatorxcode-ui-testing

Xcode Swift UI Test - Simulator hardware keyboard


I'm automating UI testing using swift in xcode and I need the hardware keyboard to always be disabled for my tests. Is there a way to make sure the hardware keyboard is disabled without human intervention, i.e. a command line script? These UI tests will be run on a build server, so manually launching a simulator and turning off the hardware keyboard is undesirable.

I've looked at xcrun simctl options to interact with the simulator, but I haven't been able to figure out how to do what I want to do. Also, I found a few SO posts indicating that what I'm trying to do is not possible, but I wasn't sure if the posts were asking exactly what I was asking.

Can I set hardware keyboard to off for iOS simulators through a shell script?


Solution

  • There is a plist with simulator preferences located at ~/Library/Preferences

    To change it to turn off the hardware keyboard, make sure the simulator is off, then run this command:

    defaults write com.apple.iphonesimulator ConnectHardwareKeyboard -bool no

    You can use xcrun simctl options in a script to shut down simulators.

    EDIT

    Apple changed this sometime in 2018-2019, I found this answer somewhere else on SO, it works for me:

    /usr/libexec/PlistBuddy -c "Print :DevicePreferences" ~/Library/Preferences/com.apple.iphonesimulator.plist | perl -lne 'print $1 if /^    (\S*) =/' | while read -r a; do /usr/libexec/PlistBuddy -c "Set :DevicePreferences:$a:ConnectHardwareKeyboard false" ~/Library/Preferences/com.apple.iphonesimulator.plist || /usr/libexec/PlistBuddy -c  "Add :DevicePreferences:$a:ConnectHardwareKeyboard bool false" ~/Library/Preferences/com.apple.iphonesimulator.plist; done