1

I have a PDF file stored in the App document Folder.

I have a PDFView as a sub-view on my viewcontroller's view. I can load the PDF from the file and display it. No problem.

However, depending on the size of the PDF, the PDFView can take a while to load.

I'm using Objective-C.

Heres my code:

- (void)loadDocument {

    _pdfView.document = [_fileFromThirdParty pdfDocument];

}

Where _pdfView is defined:

@property (strong, nonatomic) IBOutlet PDFView *pdfView;

I want to do something when the document has loaded into the view.

So, we have PDFDocument defined within PDFView. Examining both objects shows that we have notifications posted when pages are changed, etc. But I cannot find anything the fires when the document has successfully loaded into the view.

I was hoping for a delegate method like:

- (void)pdfView:(PDFView *)pdfView pdfHasLoaded:(BOOL)pdfHasLoaded {

    // Great - I can do something...!

}

I want to add a dark view to the Window (to show activity) and remove this when the PDF has loaded. I've trounced Google and SO. Nothing so obvious.

And to be clear, I'm not pulling this from a URL on a server. This is a simply load from the document bundle.

Anyone any ideas here.

Carl Hine
  • 1,817
  • 18
  • 24
  • 1
    There is no built in completion block or delegate but may be [this link](https://stackoverflow.com/questions/50527161/is-there-a-specific-way-to-listen-to-the-completion-of-pdfdocument-or-pdfview-ur) will help you. – TheTiger May 20 '19 at 11:07
  • If PDFView is not necessary then you can use `WebView` to showing the PDF and it provides the `loaded` and `failed` delegates. – TheTiger May 20 '19 at 11:09
  • Thanks. I know. I tried the WebView and you do indeed get lots of goodies out of the box for managing PDF's. But the WebView approach did not scale my PDF's very well. They appeared blurry. In fact, I was using the load delegate for this. But had to switch to using the PDFView because of it. Now, I have the load issue. But thanks for replying so quickly. – Carl Hine May 20 '19 at 12:28
  • Both of us are only me ;) – TheTiger May 20 '19 at 12:30
  • Lol - aye. I know. I'd saved my comment and quickly edited it. you must have caught sight before my edits. – Carl Hine May 20 '19 at 12:43

1 Answers1

2

You may do something like this:

func loadPDF() {

        showLoader()
        DispatchQueue.global(qos: .background).async {

            if let urlPDF = URL(string: "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf") {

                let document = PDFDocument(url: urlPDF)
                DispatchQueue.main.async(execute: {

                    self.pdfView.document = document
                    dismissLoader()
                })
            }
        }
    }
Poles
  • 3,585
  • 9
  • 43
  • 91