Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

ios - WKWebView and UIMenuController

I have an app with a WKWebView in it. In this app, I customize the options presented in the UIMenuController. The web view seems to add Copy and Define options to the menu no matter what I do. If I set myself as first responder and return NO for everything, I still get copy and define options. And I've implemented my own copy option that does special things depending on user preferences and what exactly is selected. Is there a way to remove these extra options?

Update: I've filed this as radar 18487289.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...