11

In my iOS keyboard app, I currently have a text suggestions bar much like the default iOS 8 Keyboard's suggestion bar. I would like to clear all text on the suggestion bar whenever the user does something that clears the text field (for example, when someone hits "Send" on iMessage or Whatsapp,).

As hitting "Send" is not a keystroke, I was wondering if there is way to detect from the keyboard when a textfield is cleared.

I have tried detecting empty text "" or new line "\n", but so far this has not worked.

Edit: I do know that this is possible through 3rd party iOS Keyboard, as seen the case here (this is from Themeboard)

Before hitting send, note the text in the suggestion bar.
Before hitting send

Immediately after hitting "send". The suggestion bar is cleared.
After hitting send

daspianist
  • 4,972
  • 8
  • 46
  • 87
  • I'm no expert in keyboard extension but is there any method that you can detect changes in the textField? Then you can clear if text length is zero. – SFF Jul 17 '15 at 20:52
  • @SFF This is a very good suggestion. I tried with detecting `self.textDocumentProxy.documentContextBeforeInput` (which detects what is on the textField), but unfortunately I can seem to locate the action that causes `documentContextBeforeInput` to be wiped (i.e., when Send is hit), so I could insert the appropriate method to follow with this. – daspianist Jul 18 '15 at 02:24
  • Ok, this is a horrible idea but you can set NSTimer that calls `documentContextBeforeInput` every 0.1 second. Of course it will use CPU like crazy, but at least there's a way. – SFF Jul 18 '15 at 03:39
  • @SFF Thats true, and thanks for your follow up suggestion. However, given the memory cap allocated to extensions is much lower regular apps, it would make the keyboard more prone to crashing, which would not be good :/ – daspianist Jul 19 '15 at 01:45
  • So now when you hit "Send", the message you typed was cleared but text in suggestion bar still presented, right? – SanitLee Jul 20 '15 at 16:07
  • Hi @SanitLee, yes that is correct. – daspianist Jul 20 '15 at 16:08
  • You don't have problem on your detecting empty text in text field, right? I mean you already have a method to detect that in place but just lack how to clear text in suggestion bar. – SanitLee Jul 20 '15 at 16:17
  • @SanitLee The issue is that I don't know when to call the detect empty textfield from the keyboard. Hitting "Send' is not a keystroke, so the text in the banner has to remain until the next key stroke. For example, if someone type "Hi" and pressed Send. "Hi" will remain seen on the banner until the next letter is typed. – daspianist Jul 20 '15 at 16:21

1 Answers1

11

When the send button is pressed in Messages, the textDidChange: callback will be called on your UIInputViewController subclass. At that point, both the documentContextAfterInput and documentContextBeforeInput properties of your UIInputViewController subclass's textDocumentProxy property will be nil or the empty string. While you don't know why the textfield was cleared, you can probably infer for most cases where this occurs that you should clear your current next word prediction.

- (void)textDidChange:(id<UITextInput>)textInput
{
    if ((!self.textDocumentProxy.documentContextBeforeInput && !self.textDocumentProxy.documentContextAfterInput) || ([self.textDocumentProxy.documentContextBeforeInput isEqualToString:@""] && [self.textDocumentProxy.documentContextAfterInput isEqualToString:@""])){
        //Implement code to clear the banner
        [self.keyboard.lblBanner setText: @""];
    }
}
daspianist
  • 4,972
  • 8
  • 46
  • 87
Ben Pious
  • 4,655
  • 2
  • 18
  • 30
  • This did it! Thanks man! I appended the (very simple) code solution to the answer to facilitate anyone looking for the code to incorporate into their projects. – daspianist Jul 23 '15 at 03:26