Search code examples
iosswiftios8ios-app-extensioncustom-keyboard

Why is textInput.keyboardType nil in textDidChange?


I'm developing a custom keyboard in Swift and I would like to know when the keyboard type changes (to a number or decimal keyboard type for example). I believe this should be done in either textDidChange or textWillChange. This method receives a UITextInput which has a property keyboardType. But when this method is called, that property seems to always be nil because it never runs the following code, even after I have entered a different input type (number).

override func textDidChange(textInput: UITextInput) {
    if let inputType = textInput.keyboardType {
        //never gets here
        deleteKeyboardButton.backgroundColor = UIColor.yellowColor()
    }
}

Solution

  • I found out you must get the UITextDocumentProxy and use its keyboardType property, not the textInput's property.

    var proxy = self.textDocumentProxy as UITextDocumentProxy
    if proxy.keyboardType == UIKeyboardType.EmailAddress {
        //add code here to display email input keyboard
    }