UIMenuItem #selector方法在wkwebview中崩溃

时间:2017-08-07 03:08:26

标签: crash selector wkwebview uimenuitem

UIMenuItem选择器方法在iOS 11 beta SDK中崩溃。

  

- [WKContentView highlightText]:无法识别的选择器发送到实例0x7f85df8f3200

方法定义:

func highlightText() 
{
  //
}

我尝试在WKWebView中添加UIMenuItem

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

2 个答案:

答案 0 :(得分:1)

当我覆盖canPerformAction并检查我的自定义选择器时,我也收到此错误。在我的情况下,我想删除除我的自定义菜单项以外的所有菜单项,以下内容使我的工作正常。

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
    }
}

答案 1 :(得分:0)

好的,我们终于让它适用于Swift 4:

  1. 在WKWebView子类中,添加以下属性和方法:

    // MARK: - Swizzling to avoid responder chain crash    
    var wkContentView: UIView? {
        return self.subviewWithClassName("WKContentView")
    }
    
    
    private func swizzleResponderChainAction() {
        wkContentView?.swizzlePerformAction()
    }
    
  2. 然后,为UIView添加一个扩展名(我把它放在与我的WKWebView子类相同的文件中,如果你愿意,可以使它成为fileprivate)

    // 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
        }
    }
    
  3. 现在UIMenuItem按预期工作: UIMenuItem screenshot

    但说实话,这真的感觉像是黑客,我希望Apple能解决这个问题: - /

    感谢Stephan Heilner的回答:https://stackoverflow.com/a/42985441/4670400