40

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?

Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
Tom Hamming
  • 10,577
  • 11
  • 71
  • 145
  • That is very very interesting, thank you for sharing! I've asked a similar question a while back: http://stackoverflow.com/questions/21635637/control-text-selection-behavior-in-ios-mobile-safari-and-or-uiwebview Did you ever find a solution? – Johannes Fahrenkrug Jul 06 '15 at 13:10
  • Same problem happened when I tried to set the granularity to character. thanks for this. – Bhaumik Desai Oct 13 '16 at 07:45
  • Is this problem isn't fixed yet? I can recommend to implement desired selection logic using js. – kelin Feb 20 '17 at 00:12
  • There is also this bug http://www.openradar.me/23345435 – Deniss Fedotovs Jun 23 '17 at 11:31

2 Answers2

2

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

Tom Hamming
  • 10,577
  • 11
  • 71
  • 145
1

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

Andrei Konstantinov
  • 6,971
  • 4
  • 41
  • 57