I want to disable the callout menu, which pops up if you long tap an element in the web view:
There are many answers out there like this one, but neither of them seems to work. Don't know if the things for UIWebView
are also valid for WKWebView
...
I tried to manipulate the CSS via JavaScript. This seems only work if you add your scripts to the WKUserContentController
and not on didFinish()
.
Things, which do not work:
window.getSelection().removeAllRanges();
document.body.style.webkitTouchCallout='none';
document.body.style.webkitUserSelect='none';
Things, which partly work:
var style = document.createElement('style');
style.innerHTML = '* { -webkit-touch-callout: none; -webkit-user-select: none; }';
document.getElementsByTagName('head')[0].appendChild(style);
Or in CSS form
*:not(input):not(textarea) {
-webkit-user-select: none; /* disable selection/copy of UIWebView */
-webkit-touch-callout: none; /* disable the iOS popup when long-press on a link */
}
Only things like *
or *:not(input):not(textarea)
seems to work (no body
or another specific tag). The problem with this is, that so many elements are disabled through this ... I would need this on some specific elements only!
I also tried to use canPerformAction()
:
private static readonly Selector copyAction = new Selector("copy:");
private static readonly Selector pasteAction = new Selector("paste:");
private static readonly Selector cutAction = new Selector("cut:");
public override bool CanPerform(Selector action, NSObject withSender)
{
if (action == copyAction || action == pasteAction || action == cutAction)
{
System.Diagnostics.Debug.WriteLine(action.Name);
}
return false;
}
Here it seems another responder in the chain is returning true and overwriting my settings. The popup/callout menu still appears. I was only to minimize the available options (as seen in the screen above).
The only thing I could try is to work with gesture recognizers to disable such taps, but currently I have no idea how.
What can I do to disable the popup/callout menu?