cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Re: How to implement downloading files in the background for iOS? (Swift)

How to implement downloading files in the background for iOS? (Swift)

VyacheslavBakinskiy
Helpful | Level 6

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 1

Greg-DB
Dropbox Staff
Need more support?