4

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

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

方法定义:

func highlightText() 
{
  //
}

我尝试UIMenuItem在 WKWebView 中添加,

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

2 回答 2

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
    }
}
于 2018-04-10T19:28:09.557 回答
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 子类相同的文件中,如果你愿意,可以将其设为文件私有)

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

现在 UIMenuItem 按预期工作: UIMenuItem 截图

但老实说,这真的感觉像是一个 hack,我希望 Apple 解决这个问题 :-/

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

于 2018-03-16T14:46:02.947 回答