1
votes

I am working on a PR for react-native-webview to add custom menu items to the RNCWebView (which is a subclass of WKWebView). It works in that it adds the additional options but "Copy | Look Up | Share..." always appear. Even if canPerformAction returns NO for every action (note that canPerformAction is not returning true/YES for copy)

I've come across so many other posts such as:

And countless other ones that just suggest using CSS to hide the entire menu. I am not trying to hide the whole menu, but just "Copy, Look Up and Share" while letting my custom ones remain while using WKWebView.

My thought was that there was some other class up the responder chain that is still setting them to true, but I've tried extending and overriding as many as I know about and still those options show up:


extension UIView {
    open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return false
    }
}

extension UITextView {
    open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return false
    }
}

extension UIImageView{
    open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return false
    }
}

extension UIScrollView{
    open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return false
    }
}

extension UISlider{
    open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return false
    }
}

extension UIWebView{
   open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return false
    }
}

@objc private extension UIResponder {
    func swizzle_canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return false
    }
}

None of that works. Those three options still appear no matter what. Does anyone know what class could responding and adding these options? This seems like a common question but all the posts I find are either unanswered or wildly out of date.

1
I can remove the copy, cut, selectall, paste items. I don't think you can remove the lookup and share items.El Tomato
Show me the lines where you add your custom menuItems.El Tomato
@ElTomato I add them to the WebView from this package here: github.com/react-native-webview/react-native-webview/pull/2101/…sbatson5

1 Answers

0
votes
// WKWebView subclass //
import WebKit
import UIKit

class MyWebView: WKWebView {
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return false
    }
}

// View controller //

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {
    // MARK: - Variables
    var helloMenu = UIMenuItem()
    
    
    // MARK: - IBOutlet
    @IBOutlet weak var myWebView: MyWebView!
    
    
    // MARK: - Life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        myWebView.navigationDelegate = self
        
        helloMenu = UIMenuItem(title: "Hello", action: #selector(sayHello))
        UIMenuController.shared.menuItems = [helloMenu]
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        let urlStr = "https://www.google.com/"
        if let url = URL(string: urlStr) {
            let request = URLRequest(url: url)
            myWebView.load(request)
        }
    }
    

    // MARK: - Custom menu
    @objc func sayHello() {
        print("Hello")
    }
}
  1. First, sub-class WKWebView, removing all menu items.
  2. Make an IBOutlet object of WKWebView and change its class name.
  3. Add your custom menu item to your view controller.