How does a keyboard extension know that the document proxy has changed?

1.1k views Asked by At

Keyboard extensions on iOS are subclasses of the UIInputViewController class and have the textDocumentProxy property to interact with the underlying document. The textDocumentProxy object exposes some important traits of the document, like the autocapitalization type. The question is, how does the keyboard extension know when the underlying document changes?

For example, when I tap to compose a new message in the Messages app, the “To” field uses different input traits than the message body input box. But since the keyboard doesn’t dissapear when switching focus from one field to the other, the text document proxy object changes on the fly. Can the keyboard extension notice such a change?

I’ve tried watching both the textDocumentProxy and [[self textDocumentProxy] autocapitalizationType] properties through KVO, but that doesn’t work. Checking the autocapitalizationType property using a timer reveals the change, but obviously I’d like to avoid a polling solution.

1

There are 1 answers

2
lifjoy On BEST ANSWER

My UIInputViewController supports the UITextInput protocol, which has a textDidChange method. From textDidChange, I compare self.textDocumentProxy with my own self.currentTextProxy property. When the two differ, I update self.currentTextProxy, then refresh my GUI based upon the self.textDocumentProxy’s UITextInputTraits.