UIMenuItem #selector error in wkwebview

UIMenuItem

Crash when building in iOS 11 beta SDK.

- [WKContentView highlightText]: unrecognized selector posted to instance 0x7f85df8f3200

Method definition:

func highlightText() 
{
  //
}

      

I am trying to add UIMenuItem

to WKWebView,

let menuItemHighlight = UIMenuItem.init(title: "Highlight", action: #selector(ContentWebkitView.highlightText))
UIMenuController.shared.menuItems = [menuItemHighlight]

      

+3


source to share


2 answers


OK, we finally did this for Swift 4:

  • In your WKWebView subclass, add the following property and method:

    // MARK: - Swizzling to avoid responder chain crash    
    var wkContentView: UIView? {
        return self.subviewWithClassName("WKContentView")
    }
    
    
    private func swizzleResponderChainAction() {
        wkContentView?.swizzlePerformAction()
    }
    
          

  • Then add the extension to the UIView (I put it in the same file as my WKWebView subclass, you can make it fileprivate if you like)

    // MARK: - Extension used for the swizzling part linked to wkContentView 
    extension UIView {
    
        /// Find a subview corresponding to the className parameter, recursively.
        func subviewWithClassName(_ className: String) -> UIView? {
    
            if NSStringFromClass(type(of: self)) == className {
                return self
            } else {
                for subview in subviews {
                    return subview.subviewWithClassName(className)
                }
            }
            return nil
        }
    
        func swizzlePerformAction() {
            swizzleMethod(#selector(canPerformAction), withSelector: #selector(swizzledCanPerformAction))
        }
    
        private func swizzleMethod(_ currentSelector: Selector, withSelector newSelector: Selector) {
            if let currentMethod = self.instanceMethod(for: currentSelector),
                let newMethod = self.instanceMethod(for:newSelector) {
                let newImplementation = method_getImplementation(newMethod)
                method_setImplementation(currentMethod, newImplementation)
            } else {
                print("Could not find originalSelector")
            }
        }
    
        private func instanceMethod(for selector: Selector) -> Method? {
            let classType = type(of: self)
            return class_getInstanceMethod(classType, selector)
        }
    
        @objc private func swizzledCanPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
            return false
        }
    }
    
          

The UIMenuItem now works as expected: UIMenuItem screenshot



But to be honest, it really looks like a hack and I would like Apple to fix this issue: - /

Thanks for Stefan Heilner for his answer: fooobar.com/questions/58395 / ...

0


source


I was also getting this error when I was overriding canPerformAction

and checking my custom selector. In my case, I wanted to remove all menu items except my custom one and the following did the job for me.



class ViewController: UIViewController {

    @IBOutlet weak var webView: MyWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        loadWebView()
        setupCustomMenu()
    }

    func loadWebView() {
        let url = URL(string: "http://www.google.com")
        let request = URLRequest(url: url!)
        webView.load(request)
    }

    func setupCustomMenu() {
        let customMenuItem = UIMenuItem(title: "Foo", action:
            #selector(ViewController.customMenuTapped))
        UIMenuController.shared.menuItems = [customMenuItem]
        UIMenuController.shared.update()
    }

    @objc func customMenuTapped() {
        let yay = "🤪🤪🤪🤪🤪🤪🤪🤪🤪🤪🤪🤪"
        let alertView = UIAlertController(title: "Yay!!", message: yay, preferredStyle: .alert)
        alertView.addAction(UIAlertAction(title: "cool", style: .default, handler: nil))
        present(alertView, animated: true, completion: nil)
    }
}

class MyWebView: WKWebView {
    // turn off all other menu items
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return false
    }
}

      

0


source







All Articles