2

I have WKWebView and I want to disable/remove right click menu:

enter image description here

I found similar issues:

webview load custom context menu

Cocoa webView - Disable all interaction

But I cant find

optional func webView(_ sender: WebView!,  contextMenuItemsForElement element: [AnyHashable : Any]!, 
     defaultMenuItems: [Any]!) -> [Any]!

method in WKUIDelegate or WKNavigationDelegate

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Arti
  • 7,356
  • 12
  • 57
  • 122
  • This might help: https://stackoverflow.com/questions/28801032/how-can-the-context-menu-in-wkwebview-on-the-mac-be-modified-or-overridden#28981319 – tomalbrc Aug 20 '17 at 16:22
  • Possible duplicate of [How can the context menu in WKWebView on the Mac be modified or overridden?](https://stackoverflow.com/questions/28801032/how-can-the-context-menu-in-wkwebview-on-the-mac-be-modified-or-overridden) – Tamás Sengel Sep 29 '17 at 14:06

5 Answers5

4

I figure it out as a most elegant way:

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    [webView evaluateJavaScript:@"document.body.setAttribute('oncontextmenu', 'event.preventDefault();');" completionHandler:nil];
}

Keep in mind that some JavaScripts can override this behaviour

Labokas
  • 585
  • 5
  • 7
3

For me nothing from the above worked. I know this is old, but if someone finds this years later like me, here is the solution:

Subclass WKWebView, override buildMenu(with:) and remove each item that is showing on right click, for me it was .speech and .standardEdit, so my final code looks like this:

import WebKit

class CustomWebView: WKWebView {

    override func buildMenu(with builder: UIMenuBuilder) {
        super.buildMenu(with: builder)
        
        builder.remove(menu: .speech)
        builder.remove(menu: .standardEdit)
    }
    
}

Check this documentation page for what to remove.

spasbil
  • 239
  • 1
  • 12
2

You can do this, but not from Swift/Objc side.

Intercept the 'oncontextmenu' event on your html code, for example:

<body oncontextmenu='contextMenu(event)'>

Then from javascript :

function contextMenu(evt)
{
    evt.preventDefault();
}

I deduced this from the following post that explain how to customize this menu:

How can the context menu in WKWebView on the Mac be modified or overridden?

Regards

tof
  • 21
  • 2
1

None of the JavaScript solutions above worked for me as well. @spasbil's answer works great on iOS, but for those of you looking for a solution on macOS, use willOpenMenu(_:with:) instead when subclassing WKWebView:

import WebKit

class WebView: WKWebView {
    
    override func willOpenMenu(_ menu: NSMenu, with event: NSEvent) {
        if let reloadMenuItem = menu.item(withTitle: "Reload") {
            menu.removeItem(reloadMenuItem)
        }
    }

}
AgentBilly
  • 756
  • 5
  • 20
-3
myWKView.allowsBackForwardNavigationGestures = false
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Antoine Rosset
  • 1,045
  • 2
  • 13
  • 22
  • 1
    This is not going to help in disabling the right-click event. This just disables forward and backward touch gestures in the mac/ios application. – Sanjay Pradeep Aug 27 '19 at 08:57