有没有具体的方法来监听PDFDocument或PDFView url加载的完成情况?

Joj*_*rte 3 pdfkit pdfview ios swift

我已经实现了一个自定义 PDFView,可以从云端加载 pdf 文件,也可以在本地加载 pdf 文件(如果可用)。对于本地实例,正如预期的那样,所有内容都加载得很快,但是当 url 不是本地的(即来自服务器的)时,可能需要一段时间,我想在 PDFView 加载文件时添加 UIActivityIndi​​cator,有没有办法让我们知道一个代表或一个通知来监听以跟踪这一情况?

我的实现基本上如下所示:

let url = ReportsRepository.shared.getReportUrl(id: "1234")

self.pdfView.document = PDFDocument(url: url)
Run Code Online (Sandbox Code Playgroud)

此后,如果 URL 来自服务器,则应用程序似乎冻结,因此我需要在此处添加 UIActivityIndi​​cator,问题是如何使用 PDFKit 停止它?

小智 6

我已经使用后台队列完成了。这是我的代码。

//Some loading view here
    DispatchQueue.global(qos: .background).async {

        if let url = URL(string: self.selectedWebUrl) {
            if let pdfDocument = PDFDocument(url: url) {
                DispatchQueue.main.async {
                   //Load document and remove loading view
                    self.pdfView.displayMode = .singlePageContinuous
                    self.pdfView.autoScales = true
                    self.pdfView.displayDirection = .vertical
                    self.pdfView.document = pdfDocument

                }
            } else {
                DispatchQueue.main.async {
                    //not able to load document from pdf
                    //Remove loading view

                }
            }
        } else {
            DispatchQueue.main.async {
                //Wrong Url show alert or something
                //Remove loading view

            }
        }
    }
Run Code Online (Sandbox Code Playgroud)