Search code examples
macoscocoawkwebview

WKWebView disable right click menu


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


Solution

  • 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