简体   繁体   中英

How can I get a WKWebView to show the keyboard on iOS?

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.

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?

运行前操场的屏幕截图

点按div后的游乐场屏幕截图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM