iosswiftpdfkitpdfview

How can selection be disabled in PDFView?


Displaying a PDFDocument in a PDFView allows the user to select parts of the document and perform actions e.g. "copy" with the selection. How can selection be disabled in a PDFView while preserving the possibility for the user to zoom in and out and scroll in the PDF?

PDFView itself does not seem to offer such a property nor does the PDFViewDelegate.


Solution

  • You have to subclass PDFView, as such:

    class MyPDFView: PDFView {
    
        override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
            return false
        }
    
        override func addGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer) {
            if gestureRecognizer is UILongPressGestureRecognizer {
                gestureRecognizer.isEnabled = false
            }
    
            super.addGestureRecognizer(gestureRecognizer)
        }
    
    }