iosswiftcachingnsdocumentdirectoryuidocumentinteractioncontroller

UIDocumentInteractionController: cannot use/save files


In my app, I display some remote images or PDF files and want to give the user the ability to download them. In order to do so, I try to save them locally first in .documentDirectory before opening a UIDocumentInteractionController to handle the file.

However, I am having an issue, which is that even if the action sheet opens fine and proposes all the expected options, in the end I can never use the file because of an error. Specifically:

Here is the code I use to cache the remote file, then to open it with UIDocumentInteractionController:

URLSession.shared.downloadTask(with: url) { localUrl, response, error in
    if let localUrl = localUrl {
        do {
            let imageData = try Data(contentsOf: localUrl)
            let d = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last
            if let docUrl = d?.appendingPathComponent(url.lastPathComponent) {
                try imageData.write(to: docUrl)
                self.download(docUrl)
            }
        } catch {
            print("Oops: \(error)")
        }
    }
}.resume()

func download(_ url: URL) {
    DispatchQueue.main.async {
        let documentInteractionController = UIDocumentInteractionController()
        documentInteractionController.delegate = self
        documentInteractionController.url = url
        documentInteractionController.uti = url.typeIdentifier ?? "public.data, public.content"
        documentInteractionController.name = url.localizedName ?? url.lastPathComponent
        documentInteractionController.presentOptionsMenu(from: self.view.frame, in: self.view, animated: true)
    }
}

Thank you for your help


Solution

  • I can't tell you why it doesn't work with a UIDocumentInteractionController, but it does work with a UIActivityViewController.

        private func download(_ url: URL)
        {
            DispatchQueue.main.async {
                let avc = UIActivityViewController(activityItems: [url], applicationActivities: nil)
                self.present(avc, animated: true, completion: nil)
            }
        }