Custom Keyboard TextDidChange Not working!

I am wondering why Keyboard Extension (UIInputViewController) is not acting as it is explained in documents.


In document it says "An input view controller conforms to the UITextInputDelegate protocol, allowing you to respond to changes in document content and position of the insertion point." but textWillChange OR textDidChange methods are not being called, when the active textfield text changes.


at the same time values of documentContextAfterInput OR documentContextBeforeInput are not changing if the textfield text is not changed by the proxy(for example if you use external keyboard text changes, but there is no way to monitor the change)


If you should know. calling the adjustTextPositionByCharacterOffset from proxy updates everything as it should.


I really need to monitor the text changes. looping adjustTextPositionByCharacterOffset +1 and then -1 will give me the changes but it's too much cpu usage.

Replies

It has been three years since this was reported, but I am still experiencing the same problem in Xcode 10.2.1. It occurs both in the simulator and on real devices and I have found no way to solve it. I can recreate it both in my custom keyboard project and in a brand new test project.


Bacially, "textWillChange" and "textDidChange" are not called when you type with an external keyboard, nor when you send text to the text document proxy like this:


textDocumentProxy.insertText("something")


I tried starting a scheduled timer that trigges every second, but the information in the text document proxy doesn't change, even if I insert text into it between the timer ticks.


However, changing the cursor position causes these functions to be called and also updates the document proxy information. As a test, I therefore tried doing the following in my scheduled timer:


textDocumentProxy.adjustTextPosition(byCharacterOffset: 1)
textDocumentProxy.adjustTextPosition(byCharacterOffset: -1)


This doesn't work, since the proxy must apply the change. However, if I toggle the value and adjust the position 1, -1, 1 etc. the functions are properly called and the proxy information is updated.


However, this is not an acceptable way of solving this problem. The document proxy should trigger these functions as the user types, or provide other means to detect typing.

Hi again,


I did proceed with the timer-based bug fix hack and managed to find a solution with two small flaws:


  • Since the bug fix is timer-based, there is a tiny risk that the user enters text just as the text position is changed. If so, the text will be incorrectly inserted. However, I have not been able to end up in this state, so I think it's pretty solid.
  • Since the bug fix works by adjusting the text cursor position, it will close any presented text callouts. I tried to solve this, but the very nature of the bug fix makes it very hard to fix.


This timer is part of a keyboard library that I'm building (https://github.com/danielsaidi/KeyboardKit). You can find the timer class here: https://github.com/danielsaidi/KeyboardKit/tree/master/KeyboardKit/Autocomplete


I really don't want to have to resort to this hack, but it's the best that I could come up with for now. I hope that the UIKit bug that causes these problems could be fixed soon.