打开并查看文档文件URL

时间:2017-09-27 14:05:17

标签: ios iphone

如何使用QLPreviewController打开和查看从Web服务获取的文档文件的URL。

2 个答案:

答案 0 :(得分:0)

你不能这样做。文档可以很好地解释这一点,但是,您想尝试制作自己的查看器。祝你好运!

请参阅:

Oficial topic IOS

您可以在网站上显示:“创建和配置文档交互控制器”

- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL) fileURL
usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {

UIDocumentInteractionController *interactionController =
    [UIDocumentInteractionController interactionControllerWithURL: fileURL];
interactionController.delegate = interactionDelegate;

return interactionController;}

“一旦有了文档交互控制器,就可以使用其属性来获取有关相关文件的信息,包括其名称,类型和URL。”

答案 1 :(得分:0)

  1. 显示某种进度微调器。
  2. 使用~/documentsURLSession将文件下载到URLSessionDownloadTask文件夹。
  3. 隐藏进度微调器。
  4. 然后,您需要创建一个实现QLPreviewControllerDataSource的类的实例并保存您的文档。
  5. 隐藏进度微调器并显示QLPreviewController

    import UIKit
    import QuickLook
    
    
    class Document: NSObject, QLPreviewItem {
        var previewItemURL: URL?
    }
    
    class PreviewDataSource: NSObject, QLPreviewControllerDataSource {
    
        var document: Document?
    
        func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
            return document != nil ? 1 : 0
        }
    
        func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
            return document!
        }
    }
    
    class ProgressLoadingViewController: UIViewController {
    
        var session: URLSession = URLSession(configuration: URLSessionConfiguration.default)
        var downloadTask: URLSessionDownloadTask?
    
        var dataSource: PreviewDataSource?
    
        override func viewDidLoad() {
            super.viewDidLoad()
            startDownload()
        }
    
        func startDownload() {
            // start progres spinner
            downloadTask = session.downloadTask(with: URL(string: "http://che.org.il/wp-content/uploads/2016/12/pdf-sample.pdf")!) { [weak self] (downloadLocation, response, error) in
                guard // check if download went correctly
                    error != nil,
                    let filename = response?.suggestedFilename,
                    let downloadLocation = downloadLocation
                    else {
                        print("Something went wrong: \(error!)")
                        return
                }
    
                // copy file
                let fileManager = FileManager.default
                let targetPath = URL(fileURLWithPath: filename, relativeTo: fileManager.temporaryDirectory)
                do {
                    try fileManager.copyItem(at: downloadLocation, to: targetPath)
                } catch let fileError {
                    print("Copying failed: \(fileError)")
                    return
                }
    
                // prepare preview
                let document = Document()
                document.previewItemURL = targetPath
                self?.dataSource? = PreviewDataSource()
                self?.dataSource?.document = document
    
                let qlViewController = QLPreviewController()
                qlViewController.dataSource = self?.dataSource
    
                // hide progress spinner
                self?.present(qlViewController, animated: true) {
                    self?.downloadTask = nil
                    qlViewController.reloadData()
                }
            }
            downloadTask?.resume()
        }
    }
    
  6. 当然,这个长期完成函数应该分成几个,然后移到它自己的类DocumentDownloadManager