2017-08-24 47 views
0

我们不允许存储这些文件,但允许客户自己决定是否要将其保存在不同的应用程序中或发送到某个地方。听起来像是UIDocumentInteractionController的工作,但没有持久性部分。如何在UIDocumentInteractionController中打开内存不足的文件?

里面的文档的苹果规定:

“ [...]您可以使用与已 同步到您的应用程序的文件/共享目录文件的文档交互控制器”

是否可以直接打开文件而不将其作为文件存储在UIDocumentInteractionController的文件中?

我们已经有PDF /图像数据。有没有办法将数据编码到URL并使用它来打开交互控制器?

+0

可悲的是这是不可能的。我寻找了一段时间的解决方案。问题是UIDocumentInteractionController只接受本地文件url。 –

+0

但是,也许它允许你使用NSTemporaryDirectory? –

回答

1

这不能完成,你只能共享本地资源。

你可以做的是将文件保存在临时文件夹中并访问它。以下是您如何实现这一目标的代码。结帐说明的意见:

import UIKit 

class ViewController: UIViewController { 
    let sharingController = UIDocumentInteractionController() 
    override func viewDidAppear(_ animated: Bool) { 
        super.viewDidAppear(animated) 
        if let pdfURL = Bundle.main.url(forResource: "yourPDF", withExtension: "pdf") { 
            do { 
                let pdfData = try Data(contentsOf: pdfURL) 
                // create a destination url to save your pdf data 
                let destURL = FileManager.default.temporaryDirectory.appendingPathComponent("tempFile.pdf") 
                // write your pdf data to the destinastion url 
                do { 
                    try pdfData.write(to: destURL) 
                    // share your url using UIDocumentInteractionController 
                    shareURL(url: destURL) 
                } catch { 
                    print(error) 
                } 
            } catch { 
                print(error) 
            } 
        } 
         
    } 
    func shareURL(url: URL){ 
        sharingController.url = url 
        sharingController.uti = "public.data, public.content" 
        sharingController.name = url.lastPathComponent 
        sharingController.presentOptionsMenu(from: view.frame, in: view, animated: true) 
    } 
} 

也不需要删除该文件,因为它在临时目录中。

+1

有相同的印象,可能是唯一的解决方案。猜想我们最终会这样做,并在应用程序关闭/打开时清理tmp。感谢代码共享。 – BennX

+0

感谢您的想法。我将它与注销时清除tmp目录结合起来,并使用iOS <10实现兼容性。让我们看看它是否通过了指导方针。 – BennX

+1

@BennX,np乐意帮忙。祝您好运! –

0

尝试使用,

import UIKit 
    import QuickLook 
    class YourViewController:UIViewController,QLPreviewControllerDataSource, QLPreviewControllerDelegate { 

     func openFile(){ 

     let objURL = "file URL " 
     let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String 
     let url = URL(fileURLWithPath: path) 
     let theFileName = (objURL as NSString).lastPathComponent 
     let filePath = url.appendingPathComponent("\(theFileName)").path 
     let fileManager = FileManager.default 
     if fileManager.fileExists(atPath: filePath) { 
      print("FILE AVAILABLE") 
      self.pathString = filePath 
      let viewPDF = QLPreviewController() 
      viewPDF.dataSource = self 
      viewPDF.delegate = self 
      self.item.previewItemTitle = self.previewFileTitle 
      self.item.previewItemURL = URL.init(fileURLWithPath: filePath) 
      self.present(viewPDF, animated: true, completion: nil) 
     } else { 
      print("FILE NOT AVAILABLE") 
      previewFileTitle = theFileName 
      self.downloadPDF(objURL) 
     } 


    } 

    func numberOfPreviewItems(in controller: QLPreviewController) -> Int { 
     return 1 
    } 

    func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem { 

     let url = URL(fileURLWithPath: pathString) 

     return item as QLPreviewItem 
    } 

    func previewController(_ controller: QLPreviewController, shouldOpen url: URL, for item: QLPreviewItem) -> Bool { 
     return true 
    } 
    func downloadPDF(_ url:String){ 
     let requestURL: URL = URL(string: url)! 
     let urlRequest: URLRequest = URLRequest(url: requestURL) 
     let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in 
      if let httpResponse = response as? HTTPURLResponse { 
       let statusCode = httpResponse.statusCode 
       if (statusCode == 200) { 
        print("Downloaded Successfully") 
        let documentsDirectoryUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first 
        let sourceUrl = URL(string:url)! 
        //Get the file name and create a destination URL 
        let fileName = sourceUrl.lastPathComponent 
        let destinationURL = documentsDirectoryUrl!.appendingPathComponent(fileName) 

        //Hold this file as an NSData and write it to the new location 
        if let fileData = try? Data(contentsOf: sourceUrl) { 
         try? fileData.write(to: destinationURL, options: []) // true 
         print(destinationURL.path) 
         self.pathString = destinationURL.path 
         let viewPDF = QLPreviewController() 
         viewPDF.dataSource = self 
         viewPDF.delegate = self 
         self.item.previewItemTitle = self.previewFileTitle 
         self.item.previewItemURL = destinationURL 
         self.present(viewPDF, animated: true, completion: nil) 
        } 

       }else{ 
       } 
      } 
     } 
     task.resume() 

    } 
} 

class PreviewItem: NSObject, QLPreviewItem { 
    var previewItemURL: URL? 
    var previewItemTitle: String? 
} 

Developer documentation
See this tutorial

相关问题