When a user taps and holds to select a word, then moves their finger towards the top or bottom edges of the screen, the page automatically scrolls to adjust for the selection.
Watch a short clip demonstrating this
I am looking to disable this behavior within a WKWebView
.
Here is what I have attempted so far:
In a bridge.js
file that is accessible to the webview:
var shouldAllowScrolling = true;
document.addEventListener('selectionchange', e => {
shouldAllowScrolling = getSelectedText().length === 0;
window.webkit.messageHandlers.selectionChangeHandler.postMessage(
{
shouldAllowScrolling: shouldAllowScrolling
});
console.log('allow scrolling = ', shouldAllowScrolling);
});
And in a WKScriptMessageHandler
implementation:
public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage)
{
switch message.name
{
case "selectionChangeHandler":
let params = paramsDictionary(fromMessageBody: message.body)
let shouldEnableScrolling = params["shouldAllowScrolling"] as? Bool ?? true
cell?.webView.scrollView.isScrollEnabled = shouldEnableScrolling
cell?.webView.scrollView.isUserInteractionEnabled = shouldEnableScrolling // not together with the line above
default:
fatalError("\(#function): received undefined message handler name: \(message.name)")
}
}
Similarly, I have tried using the preventDefault()
function directly in the javascript file for events such as scroll
and touchmove
:
document.addEventListener('touchmove', e => {
if (!shouldAllowScrolling) {
e.preventDefault()
}
}, {passive: false});
While both methods successfully prevent scrolling when text is selected, they do not override the initial behavior mentioned at the beginning of my question.
I am open to solutions involving Swift, JavaScript, or a combination of both.