Search code examples
objective-cios8ios-app-extensioncustom-keyboard

Detect keyboard type changes in custom keyboard for iOS 8


I'm currently creating an iOS 8 custom keyboard extension, and I'm looking for a way to determine when the users switch to another input, in order to change my layout.

For example, when a users selects a UITextField with type UIKeyboardTypeEmailAddress I intend to present a custom keyboard, and when the user selects another UITextField with type UIKeyboardTypeDecimalPad, I want to notice it, and update my keyboard's layout. How does one get notified when the keyboard type changes in order to update the keyboard layout?


Solution

  • You can detect changes to the keyboard type in textDidChange. You need to get the UITextDocumentProxy then check the proxy's keyboardType. If it's a keyboard type you want to support, you can then present the appropriate UI. For example, this is how you would detect when the email keyboard should be displayed:

    override func textDidChange(textInput: UITextInput) {
        // Called when the document context is changed - theme or keyboard type changes
    
        var proxy = self.textDocumentProxy as UITextDocumentProxy
        if proxy.keyboardType == UIKeyboardType.EmailAddress {
            //add code here to display email input keyboard
        }
    }