2017-09-27 78 views

回答

0

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

参见:

Oficial topic IOS

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

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

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

return interactionController;} 

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

+0

当然,你可以做到这一点。这很简单。 – orkoden

+0

是的,但创建你自己的脚本。没有资源明确的语言。 –

+0

查看我的回答以上https://stackoverflow.com/a/46451323/1329214 – orkoden

0
  1. 显示某种进度微调。
  2. 使用URLSessionURLSessionDownloadTask将文件下载到您的~/documents文件夹。
  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() 
        } 
    } 
    

当然这漫长完成函数应该分成几个,然后搬到了自己的类DocumentDownloadManager

+0

你能回答我的目标C吗? @orkoden – monali