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
I can't tell you why it doesn't work with a
UIDocumentInteractionController
, but it does work with a UIActivityViewController.