2017-09-27 77 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