We’re Still Here to Help (Even Over the Holidays!) - find out more here.
Forum Discussion
VyacheslavBakinskiy
6 years agoHelpful | Level 6
How to implement downloading files in the background for iOS? (Swift)
Hello, I'm using file download from Dropbox in my app. It works fine when app is active, but if my app goes into the background the download stops. How can I fix it? I use this code to download:
private let client = DropboxClientsManager.authorizedClient
func downloadFile(_ file: TrackModelProtocol?, callback: @escaping () -> ()) {
guard let path = file?.path, let pathComponent = file?.fileName else { return }
if let client = client {
let fileManager = FileManager.default
let directoryURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let destURL = directoryURL.appendingPathComponent(pathComponent)
let destination: (URL, HTTPURLResponse) -> URL = { temporaryURL, response in
return destURL
}
client.files.download(path: path, overwrite: true, destination: destination)
.response { _ , error in
if let error = error{
print("Downloading from Dropbox fails: \(error)")
return
}
//Call some action after the download is complete
callback()
}
}
}
1 Reply
Replies have been turned off for this discussion
- Greg-DB6 years ago
Dropbox Community Moderator
I'm closing this as a duplicate of: https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Downloading-in-the-background-in-iOS-Swift/td-p/467860
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!