ios - 如何使用QLPreviewController快速显示远程文档

标签 ios swift document

我正在使用QLPreviewController预览文档。但是我不知道如何显示存储在服务器上的文档。

最佳答案

你不能QuickLook仅适用于本地资源文件。您需要首先异步下载数据,将其保存到文档目录或临时文件夹中,并在完成后从主线程显示QLPreviewController:

import UIKit
import QuickLook
class ViewController: UIViewController, QLPreviewControllerDataSource {
    let preview = QLPreviewController()
    let tempURL = FileManager.default.temporaryDirectory.appendingPathComponent("quicklook.pdf")
    func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
        return 1
    }
    func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
        return tempURL as QLPreviewItem
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        preview.dataSource = self
        preview.currentPreviewItemIndex = 0
        let url = URL(string:"https://images.apple.com/environment/pdf/Apple_Environmental_Responsibility_Report_2017.pdf")!
        URLSession.shared.dataTask(with: url) { data, response, error in
            guard let data = data, error == nil else {
                //  in case of failure to download your data you need to present alert to the user and update the UI from the main thread
                DispatchQueue.main.async {
                    UIApplication.shared.isNetworkActivityIndicatorVisible = false
                    let alert = UIAlertController(title: "Alert", message: error?.localizedDescription ?? "Failed to download the pdf!!!", preferredStyle: .alert)
                    alert.addAction(UIAlertAction(title: "OK", style: .default))
                    self.present(alert, animated: true)
                }
                return
            }
            // write the downloaded data to a temporary folder or to the document directory if you want to keep the pdf for later usage
            do {
                try data.write(to: self.tempURL, options: .atomic)   // atomic option overwrites it if needed
                // you neeed to check if the downloaded data is a valid pdf
                // and present your controller from the main thread
                DispatchQueue.main.async {
                    UIApplication.shared.isNetworkActivityIndicatorVisible = false
                    if self.tempURL.typeIdentifier == "com.adobe.pdf" {
                        self.present(self.preview, animated: true)
                    } else {
                        print("the data downloaded it is not a valid pdf file")
                    }
                }
            } catch {
                print(error)
                return
            }

        }.resume()
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
    }
}

extension URL {
    var typeIdentifier: String? {
        return (try? resourceValues(forKeys: [.typeIdentifierKey]))?.typeIdentifier
    }
}

关于ios - 如何使用QLPreviewController快速显示远程文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57387808/

相关文章:

iOS 绘制简单的条形图像 Mint 应用程序

ios - 使用 notificatincenter 处理用户键入通知时出现问题

ios - 仅在点击时选择 UIPickerView 中的多行(而不是滚动到一行时)

swift - 如何在 SwiftUI 中禁用甚至删除 macOS 中“ View ”菜单中的 EnterFullScreen 选项?

ios - Swift: map View 消失/重新出现后,MapView geocodeAddressString 不起作用

html - 如何清理微软 html 文档?

security - 无法在 Firefox 24 上将文档域设置为 amazonaws

ios - 为 swift 添加 POST、PUT、DELETE 请求类

ios - corebluetooth 和 ios 状态

Objective-C 标准文档