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.
My
UIInputViewController
supports theUITextInput
protocol, which has atextDidChange
method. FromtextDidChange
, I compareself.textDocumentProxy
with my ownself.currentTextProxy
property. When the two differ, I updateself.currentTextProxy
, then refresh my GUI based upon theself.textDocumentProxy
’sUITextInputTraits
.