1

After selecting text in a textview/textfield in an app (Messages, for example), you can select Share from the popup menu to share the text. Since you were editing text at the time you selected/shared, the keyboard is visible. Once you have selected an share target app, the keyboard will continue to be visible when the target app's extension appears. I need to dismiss this keyboard so that other elements on the screen are visible and accessible. In my case, I have no editable fields (e.g. textField/textView) in the custom view controller that appears in the extension. (i.e. to become firstResponder)

The usual solutions that do not work:

  1. Use the Dismiss button in the KB - does not exist on iPhone
  2. [self.view endEditing:YES] - No subview in my view is firstResponder
  3. [self.textView resignFirstResponder] - No textView/textField in my view (https://stackoverflow.com/a/28119785/650365)
  4. [[[UIApplication sharedApplication] keyWindow] endEditing:YES] - sharedApplication is unavailable in share extensions
  5. [theViewController setEditing:NO animated:YES]

Hacky solution that does work (from https://stackoverflow.com/a/34299724/650365):

  • Add a textField to your VC in the storyboard/XIB and set a reference outlet in your .m file. (You can place it off-screen or mark it Hidden.)
  • In viewDidLoad, run becomeFirstResponder.
  • In viewDidAppear, run resignFirstResponder on that same textField.

How can I dismiss the keyboard in a Share Extension on iPhone without adding extra textField/textViews?

Community
  • 1
  • 1
Maple
  • 741
  • 13
  • 28
  • Did you try to dismiss the keyboard using the methods you mentioned in the viewWillDisappear of viewDidDisappear? – Alex Nov 17 '16 at 02:30
  • At button action of share call `[self.view endEditing:YES]` – Vinodh Nov 17 '16 at 05:08
  • @Alex Which VC's viewWillDisappear are you thinking? The sharing app (that is disappearing) is not under my control. Is there an interstitial VC you know of that I can hook into, perhaps as a delegate? – Maple Nov 18 '16 at 16:54
  • @Vinodh I don't have control of the sharing app (where the action is initiated), only the receiving app. – Maple Nov 18 '16 at 16:54
  • If by sharing app you mean a different application which is in background, in this case I am surprised the keyboard was not resigned automatically by the system. keyWindow contains the current window, which probably is not the one that launched the keyboard. On the other hand, the [[[UIApplication sharedApplication] windows] array should contain the list of all the windows, hidden and visibles. May be you can try to iterated through the "windows" array and resign the first responder on all of them. – Alex Nov 18 '16 at 23:55
  • @Alex - Yeah, it seems all the good magic tricks originate from `[UIApplication sharedApplication]`. Unfortunately as I said in #4 in the Q, sharedApplication is unavailable in share extensions. For sandboxing reasons, I'd guess. – Maple Nov 21 '16 at 20:47

1 Answers1

0

If you see “First Responder” in the MainInterface.storyboard enter image description here

then calling self.resignFirstResponder inside viewWillAppear will dismiss the keyboard.

regina_fallangi
  • 2,080
  • 2
  • 18
  • 38