Reputation: 1648
My app requieres to download and show files in a QLPreviewController
. In some situations, this files must be temporary, that is, after user finish the QLPreviewController, files should be deleted from internal storage.
I was trying to call a completion block removing these files as the following code shows:
func launchPreview(QLpreviewItem: QLPreviewItem, completion: @escaping () -> Void) {
previewItem = QLpreviewItem
let previewController = QLPreviewController()
previewController.dataSource = self
self.present(previewController, animated: true, completion: {
completion()
})
}
The completion block looks like that and it's working fine:
if fileManager.fileExists(atPath: path) {
try! fileManager.removeItem(atPath: path)
}
The problem with this approach is when users tries to share the file on the previewController... It was removed just after show it on the preview controller, so it is not available.
Is there any way to execute code after previewController is dismissed?
Alternatively, what about to store files on a temporary directory? It will be possible on iOS?
I'm using the following code to download and save my files:
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] //path
let fileURL = documentsURL.appendingPathComponent(attachment.name) //url
return (fileURL, [.createIntermediateDirectories])
}
Alamofire.download(RestHelper.apiV1 + "/attachments/\(attachment.id)", to: destination).response { response in
//Some code here
}
Maybe it is more simple to change the destination to a tmp directory, but I'm not sure.
Thanks!
Upvotes: 2
Views: 938
Reputation: 55815
QLPreviewControllerDelegate
can inform you when the preview controller will/did dismiss if you implement the appropriate function.
optional func previewController{Will/Did}Dismiss(_ controller: QLPreviewController)
Just remember to set the delegate on QLPreviewController
:
let preview = QLPreviewController()
preview.dataSource = self
preview.delegate = self
Upvotes: 1