Apple PDFKit Notifications not working properly

Viewed 863

I try to get notifications as soon as the currently visible pages in my pdfView change.

Actually this should work according to the documentation via .PDFViewVisiblePagesChanged.

However, I only get a notification the very first time when the pdfView didLoad.

As soon as I then scroll (and thus the visible pages change) I do not receive a single notification. The funny thing is that the notification .PDFViewPageChanged works normally (but in this case it is not enough). The same behaviour is also with .PDFViewScaleChanged: here I never get a notification when zooming inside the pdfView.

Code Snippet from my ViewController:

ovverride func viewDidLoad() {
    //...
    NotificationCenter.default.addObserver(self, selector: #selector(onDidVisiblePagesChanged(_:)), name: .PDFViewPageChanged, object: nil)
    //...
}
@objc func onDidVisiblePagesChanged(notification:Notification) {
   print("visible Pages changed!")
}
1 Answers

The problem is located in your function "onDidVisiblePagesChanged, you're waiting for a parameter type notification, you need to cast the parameter inside your function

@objc func onDidVisiblePagesChanged(_ sender:Any) {
    if let notification = sender as? Notification {
        print("visible Pages changed!")
        print(notification)
    }
}
Related