Thanks(https://i.stack.imgur.com/Nz2fD.png)

I try to implement this method of WKUIDelegate, but it doesn't work

@available(iOS 13.0, *)
public func webView(_ webView: WKWebView, contextMenuConfigurationForElement elementInfo: WKContextMenuElementInfo, completionHandler: @escaping (UIContextMenuConfiguration?) -> Void) {
    completionHandler(nil)
}
1

There are 1 answers

2
Chandaboy On

you need to implement these functions in your controller, check these links for reference Disable entire UIMenuController edit menu in WKWebView

WKWebView and UIMenuController

override func viewDidLoad() {
super.viewDidLoad()
       NotificationCenter.default.addObserver(
                         self,
                         selector: #selector(uiMenuViewControllerDidShowMenu),
                         name: NSNotification.Name.UIMenuControllerDidShowMenu,
                         object: nil)
}

deinit

deinit {
    NotificationCenter.default.removeObserver(
                       self,
                       name: NSNotification.Name.UIMenuControllerDidShowMenu,
                       object: nil)
    }


func uiMenuViewControllerDidShowMenu() {
        if longPress {
            let menuController = UIMenuController.shared
            menuController.setMenuVisible(false, animated: false)
            menuController.update() //You can only call this and it will still work as expected but i also call setMenuVisible just to make sure.
        }
    }


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

}