0

I want to disable the callout menu, which pops up if you long tap an element in the web view:

Cut, copy, paste, look up, share

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?

testing
  • 19,681
  • 50
  • 236
  • 417
  • Try to open the webview in Safari to check if the issue still exists . You could also share the sample so that I can test it on my side . – Lucas Zhang Mar 26 '21 at 04:37
  • OK I'll make some tests and if I find something I'll let you know. Which sample do you need? The Xamarin one, the web page or both? I can't really give you the web page, but you can try [this link](https://material.angular.io/components/tooltip/overview) and long tap the "Action" button with an iPhone (iOS 14) device. This should simulate the problem. – testing Mar 26 '21 at 09:00

1 Answers1

0

I have tried overriding the method canPerformAction which is working well in UIWebView, but it does not work on WKWebView.

    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    if action == #selector(UIResponderStandardEditActions.cut(_:)) ||
        action == #selector(UIResponderStandardEditActions.copy(_:)) ||
        action == #selector(UIResponderStandardEditActions.selectAll(_:)) ||
        action == #selector(UIResponderStandardEditActions.select(_:)) {
        return false
    }
    return super.canPerformAction(action, withSender: sender)
}
user550088
  • 712
  • 1
  • 8
  • 16