Wkwebview Allowslinkpreview to False Breaks Text Selection

SwiftUI WKWebView disable link interactions

Here is correct delegate callback

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.navigationType == .linkActivated {
decisionHandler(.cancel)
} else {
decisionHandler(.allow)
}
}

depending of used HTML, probably also might be disabled .formSubmitted, if needed.

Disable link drag on WKWebView

I found the solution here: How to disable iOS 11 and iOS 12 Drag & Drop in WKWebView?

This is my working code:

private func disableDragAndDropInteraction() {
var webScrollView: UIView? = nil
var contentView: UIView? = nil

if #available(iOS 11.0, *) {
guard let noDragWebView = webView else { return }
webScrollView = noDragWebView.subviews.compactMap { $0 as? UIScrollView }.first
contentView = webScrollView?.subviews.first(where: { $0.interactions.count > 1 })
guard let dragInteraction = (contentView?.interactions.compactMap { $0 as? UIDragInteraction }.first) else { return }
contentView?.removeInteraction(dragInteraction)
}
}
override func viewDidAppear(_ animated: Bool) {
disableDragAndDropInteraction()
}

You also have to add disableDragAndDropInteraction() after initialising WKWebView, e.g. in viewDidLoad().

WKWebView selection haptics when selection is turned off

I'm having the same issue and I found something that seems to work just fine:

let longPress:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: nil, action: nil)
longPress.minimumPressDuration = 0.2
webView.addGestureRecognizer(longPress)

Taken from Disabling user selection in UIWebView.



Related Topics



Leave a reply



Submit