For iOS 11, simply subclass WKWebView
and override canPerformAction
to return false
:
class WebView : WKWebView {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return false
}
}
For iOS 10 or earlier, swizzle WKContentView
's canPerformAction
method:
@objc private extension UIResponder {
func swizzle_canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return false
}
}
guard let viewClass = NSClassFromString("WKContentView") as? UIView.Type else { return }
method_exchangeImplementations(
class_getInstanceMethod(viewClass, #selector(UIResponder.canPerformAction))!,
class_getInstanceMethod(UIResponder.self, #selector(UIResponder.swizzle_canPerformAction))!
)
After remove those web view's build-in menu items, you can add your custom menu items via UIMenuController.shared
like normal.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…