10

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.

Tom Hamming
  • 10,577
  • 11
  • 71
  • 145
  • 1
    I've same issue, any progress? – avdyushin Jun 08 '15 at 12:21
  • @avdyushin no. But I am at WWDC. Going to ask someone this week. – Tom Hamming Jun 08 '15 at 16:04
  • @avdyushin it's a known issue but no timeline. The full fix for now is to hide the menu on willShow and make and present your own with whatever options you want. – Tom Hamming Jun 15 '15 at 14:49
  • Thanks, will use `UIMenuControllerWillShowMenuNotification` notification... – avdyushin Jun 16 '15 at 04:56
  • 1
    hi guys, any updates on this? I try to call menuVisible=NO UIMenuControllerWillShowMenuNotification but it is not hiding the default copy/define/share... menu – mkto Oct 06 '15 at 09:22
  • Can you please elaborate on the "make and present your own" portion of your current work around? Do you mean create your own menu from scratch? Somehow manage UIMenuController independently? Or using something like QBPopupMenu (https://github.com/questbeat/QBPopupMenu)? Cc: @avdyushin – davew Oct 19 '15 at 23:47
  • I'm using QBPopupMenu. On `WillShow` i've created dummy view outside of screen, and set target rect for UIMenuController in this view. (It will hides system menu). Then show QBPopupMenu on `DidShow` where I want. – avdyushin Oct 26 '15 at 08:33
  • Check my answer in [here](http://stackoverflow.com/a/42510788/4990431) you can get frame of UIMenuController and implement your view after original UIMenuController fades away. – ysnzlcn Feb 28 '17 at 14:12
  • @davew the issue appears to be resolved in iOS 13. – Tom Hamming Jun 10 '19 at 18:37

2 Answers2

3

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.

Jonny
  • 1,969
  • 18
  • 25
1

This appears to be fixed in iOS 13 beta 1.

Tom Hamming
  • 10,577
  • 11
  • 71
  • 145