UIDocumentInteractionController: cannot use/save files

1.4k Views Asked by At

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:

  • If I try to use the file in a mail, the mail opens but empty,
  • If I try to use it in Whatsapp, I get an error saying "The item cannot be shared. Please selected a different item."
  • And if I choose "Save to Files", the files action sheet briefly opens but closes immediately afterwards with an error in the console saying: [ShareSheet] cancelled request - error: The operation couldn’t be completed. Invalid argument

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

2

There are 2 best solutions below

2
On BEST ANSWER

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)
        }
    }
0
On

I had the same issue with the UIDocumentInteractionController. What solved the issue for me, was to hold onto the reference to the UIDocumentInteractionController in a field in my ViewController like so:

class AdlerShopViewController: UIViewController {
    private var documentInteractionController: UIDocumentInteractionController?

    private func shareFile(at url: URL) {
        DispatchQueue.main.async { [self] in
            documentInteractionController = UIDocumentInteractionController()
            documentInteractionController?.url = url
            documentInteractionController?.name = url.lastPathComponent
            documentInteractionController?.presentOptionsMenu(from: view.frame, in: view, animated: true)
        }
    }
}