Search code examples
iosiphonexcodeswiftuitest

how to Detect if Keyboard is shown in Xcode UI test


I am writing a UI text in swift under the new Xcode 7 UI test framework. the requirement is to test whether the system keyboard is shown in an app. can someone give me a clue on how to do that? thanks


Solution

  • Add two observers

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardVisible:", name: UIKeyboardDidShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardHidden:", name: UIKeyboardDidHideNotification, object: nil)
    
    func keyboardVisible(notif: NSNotification) {
        print("keyboardVisible")
    }
    
    func keyboardHidden(notif: NSNotification) {
        print("keyboardHidden")
    }
    

    Whenever the keyboard is visible keyboardVisible will be called and whenever the keyboard is hidden keyboardHidden will be called.