Search code examples
iosswiftnotificationspdfkit

Apple iOS PDFKit's PDFViewPageChanged notification does not work iOS both 15.2 and 15.4


Whenever the current page changes by scrolling the pdf view in PDFkit, apps can detect the change adding the observer with PDFViewPageChanged.

The related sample code is as below,

// Add page change observer
NotificationCenter.default.addObserver(self,
            selector: #selector(pageDidChange(notification:)),
              name: Notification.Name.PDFViewPageChanged,
              object: nil)

@objc private func pageDidChange(notification: Notification) {
     // pdfView is of type PDFView
     let currentPageNumber = pdfView.document?.index(for: pdfView.currentPage!)
}

From How to know when user swipes to next page in PDFView of PDFKit?

PDFViewPageChanged works well in iOS 15.0 and before. However, in case of iOS 15.2 and 15.4 (or later, latest)a function bound to PDFViewPageChanged is not called when the page changes.

I googled tons of pages to solve it. I couldn't find any solution or hints. Please help me. It's a very essential and fundamental function. So it's very strange.


Solution

  • Apple iOS PDFKit's PDFViewPageChanged notification was not delivered because a scrollview was added on the pdfview and the methods of the scrollview's delegate were overrode.

    Before iOS 15.2, even though methods of the delegate were overrode, PDFKit's observer worked correctly.

    However, since 15.2, in this case, the observer does not work well. I removed the added scollview and added gesture recognizers instead to handle some events like tapping and panning.