简体   繁体   中英

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!

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
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM