0

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

Another Dude
  • 1,204
  • 4
  • 15
  • 33
  • How do you know that imageData has already been available when you use download(_ url: URL)? – El Tomato Jun 24 '21 at 08:17
  • Have you ever met a process guy named 'asynchronous'? – El Tomato Jun 24 '21 at 08:20
  • Have you tried using a `UIActivityViewController `? `let avc = UIActivityViewController(activityItems: [url], applicationActivities: nil) present(avc, animated: true, completion: nil)` – D. Mika Jun 24 '21 at 10:46

2 Answers2

1

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)
        }
    }
D. Mika
  • 2,577
  • 1
  • 13
  • 29
  • Thank you for that, it works. I'm still having a strange issue that I explained here with `UIDocumentInteractionController`, which is that the first time I call `UIActivityViewController`, it takes a lot of time (like 4-5 seconds) to pop. But still this works! – Another Dude Jun 24 '21 at 20:40
  • 1
    @AnotherDude: I have the same problem. I haven't figured out what is causing this yet. – D. Mika Jun 25 '21 at 09:33
1

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)
        }
    }
}
cybergen
  • 3,108
  • 1
  • 22
  • 30