disable iOS simulator 'connect hardware keyboard' programmatically

前端 未结 2 1432
天命终不由人
天命终不由人 2020-12-10 15:31

I\'m writing XCUITests for view has 2 UITextField and by defualt the simulator has hardware keyboard connected, hence the software one is not being presented.

Is it

相关标签:
2条回答
  • 2020-12-10 15:52

    To disable iOS Simulators Connect Hardware Keyboard setting (Simulator -> Hardware -> Connect Hardware Keyboard), add this sniped in AppDelegate. ref link

    #if targetEnvironment(simulator)
    // Disable hardware keyboards.
    let setHardwareLayout = NSSelectorFromString("setHardwareLayout:")
    UITextInputMode.activeInputModes
        // Filter `UIKeyboardInputMode`s.
        .filter({ $0.responds(to: setHardwareLayout) })
        .forEach { $0.perform(setHardwareLayout, with: nil) }
    #endif
    
    0 讨论(0)
  • 2020-12-10 15:57

    Went through the same problem. Found a simple solution to add a prescript in the UI tests scheme.

    First, this is image of the plist file opened with Xcode. This is located at ~/Library/Preferences/com.apple.iphonesimulator.plist

    Now here is the prescript I added to my ui tests scheme. Read the comments in script for further explanation.

    killall Simulator
    defaults write com.apple.iphonesimulator ConnectHardwareKeyboard -bool false
    

    How it works First the simulator is killed. Next the "defaults" command writes the key/value into the simulators plist. Now when the tests start, they launch the simulator which takes into account the value we wrote in plist.

    Hope it works for you too. :)

    0 讨论(0)
提交回复
热议问题