I am seeking a solution to deactivate the callout menu that appears when an element is long-tapped in a web view:
https://i.sstatic.net/TiWsY.png
Despite various answers available, like this one, none seem to be effective. It's unclear whether the techniques for UIWebView
are also applicable to WKWebView
...
I attempted to manipulate the CSS using JavaScript. However, this only seems to work if scripts are added to the WKUserContentController
and not within didFinish()
.
The following methods do not produce desired outcomes:
window.getSelection().removeAllRanges();
document.body.style.webkitTouchCallout='none';
document.body.style.webkitUserSelect='none';
Some partially successful approaches include:
var style = document.createElement('style');
style.innerHTML = '* { -webkit-touch-callout: none; -webkit-user-select: none; }';
document.getElementsByTagName('head')[0].appendChild(style);
Alternatively, 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 wildcard selector *
or *:not(input):not(textarea)
appear to be effective (not specific tags like body
). Unfortunately, these methods disable many elements indiscriminately ... I require this functionality on specific elements only!
I also experimented with 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;
}
In this case, it appears that another responder in the chain is overriding my settings by returning true. The popup/menu continues to appear. My efforts have only been able to limit the available options (as shown in the screenshot).
My next thought is to utilize gesture recognizers to prevent such taps, although I lack knowledge in this area at present.
What steps can I take to successfully disable the popup/callout menu?