如何在swift中使用QLPreviewController显示远程文档 [英] How to display remote document using QLPreviewController in swift

查看:539
本文介绍了如何在swift中使用QLPreviewController显示远程文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using QLPreviewController to preview documents. But i do not know how to display document stored on a server.

推荐答案

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

You can't. QuickLook only works for local resource files. You would need to download the data asynchronously first, save it to the document directory or to a temporary folder and present the QLPreviewController from the main thread when finished:

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
    }
}

这篇关于如何在swift中使用QLPreviewController显示远程文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆