Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS: Text Selection in WKWebView (WKSelectionGranularityCharacter)

I've got an app that uses a web view where text can be selected. It's long been an annoyance that you can't select text across a block boundary in UIWebView. WKWebView seems to fix this with a property on its configuration: selectionGranularity. One of the possible values is WKSelectionGranularityCharacter:

Selection endpoints can be placed at any character boundary.

Sounds great! Exactly what I need. Except that when I set that in my web view, I often can no longer select text at all. What is going on? Is there something else I need to set? Has anyone figured this out?

Update: I've figured out the following bugs:

  1. When there is more than one WKWebView in an app with selectionGranularity set to WKSelectionGranularityCharacter, only the most recent one to load can select text. I've filed this as bug 18441138.

  2. If there is a click handler attached to an element inside the body in the HTML content of a WKWebView whose selectionGranularity is set to WKSelectionGranularityCharacter, text selection doesn't work inside that element. I've filed this as bug 18440833.

  3. Text selection fails in the WKWebView after you've entered edit mode on a UITextView somewhere else in the app until the WKWebView reloads. I've filed this as bug 18468405.

Has anyone worked around any of these?

like image 567
Tom Hamming Avatar asked Sep 19 '14 16:09

Tom Hamming


2 Answers

These and other issues are all fixed as of iOS 13.

like image 62
Tom Hamming Avatar answered Oct 14 '22 03:10

Tom Hamming


There are several known bugs that are still not fixed.

You can execute JavaScript to get selected text as a workaround.

window.getSelection().toString()

Docs

like image 26
Andrei Konstantinov Avatar answered Oct 14 '22 04:10

Andrei Konstantinov