shochu_king
shochu_king

Reputation: 136

How to disable look up, share, forward menu items in pdfkits pdfview in ios13

Since the ios13 update, overriding the PDFView class’s method canPerformAction(_ action: Selector, withSender sender: Any?) no longer detects or controls the "look up", "share" and "forward" menu items and I can't figure out a way to disable them. Previously overriding it in this manner blocked all menu items:

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return false
}

However this only blocks cut, copy, paste from ios13. Has anyone figured this out? If so I would very much appreciate your help!

Upvotes: 2

Views: 1583

Answers (3)

john07
john07

Reputation: 560

You can override buildMenu for PDFView subclass

override func buildMenu(with builder: UIMenuBuilder) {
    builder.remove(menu: .share)
    builder.remove(menu: .lookup)
    super.buildMenu(with: builder)
}

Upvotes: 1

somedev
somedev

Reputation: 801

I faced the same issue. Looks like in iOS13 some actions (menu and touches) are delegated to PDFView's property documentView. So, method swizzling for that view class worked for me, but it looks like a working but "dirty" hack. In my PDFView subclass I've added:

private func swizzleDocumentView() {
    guard let d = documentView, let documentViewClass = object_getClass(d) else { return }

    let sel = #selector(swizzled_canPerformAction(_:withSender:))
    let meth = class_getInstanceMethod(object_getClass(self), sel)!
    let imp = method_getImplementation(meth)

    let selOriginal = #selector(canPerformAction(_:withSender:))
    let methOriginal = class_getInstanceMethod(documentViewClass, selOriginal)!

    method_setImplementation(methOriginal, imp)
}

@objc func swizzled_canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
   return false
}

Upvotes: 1

The_Falcon
The_Falcon

Reputation: 287

Figure out what class the elements you are selecting belong to (UITextField, UITextView, UIWebView, etc.), and method swizzle its canPerformAction:withSender:.

Upvotes: 1

Related Questions