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:
- Telling the web view to
becomeFirstResponder
(a long shot, because the web view wouldn't know whatdiv
to use). - Injecting JS to tell the
div
tofocus()
. (This works in other browsers, but sadly not inWKWebView
) - Simulating touch events in JS via
TouchEvent
anddispatchEvent()
in the hope of making it seem that the user had tapped on thediv
.
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.