2

My iOS app uses a WKWebView with contenteditable = true on a specific div. I'd like to have code to make the keyboard show up for the web view, so the user can just start typing. Things I've tried that have had no effect:

  1. Telling the web view to becomeFirstResponder (a long shot, because the web view wouldn't know what div to use).
  2. Injecting JS to tell the div to focus(). (This works in other browsers, but sadly not in WKWebView)
  3. Simulating touch events in JS via TouchEvent and dispatchEvent() in the hope of making it seem that the user had tapped on the div.

In the third case I also used addEventListener() to observe the simulated touches and compare them to real touch events from tapping the screen. It looks like the key difference is that the event's isTrusted value is false for the simulated touches.

I get that it's a potential security issue to let apps simulate touch events, but I didn't have any other ideas. I'm trying to get the keyboard to appear, what the user types is up to them and not something I want to mess with. Basically I want the same thing as calling becomeFirstResponder() on a UITextView.

This is very similar to a WebKit issue 142757 but I haven't figured out how to use the suggested workaround linked from there.

Clarification: I can set up and use an editable web view, but the keyboard doesn't appear until I tap on the web view. I'm trying to make the keyboard appear automatically, without requiring a tap to initiate editing.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170

1 Answers1

0

I tried this in an iPad playground, and it works without any action on my part. It’s possible there is another view that is capturing touches, or “contenteditable” is misspelled, or something else?

screenshot of playground before running

screenshot of playground after tapping div

Aaron Kreipe
  • 309
  • 3
  • 8
  • 4
    Using that code, the keyboard does not appear for me until I tap on the web view. I can do that part-- the question is, how do I get the keyboard to appear **from code** without requiring a tap? – Tom Harrington Sep 07 '18 at 20:11