cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Share your feedback on the Document Scanning Experience in the Dropbox App right 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: 

Downloading in the background in iOS (Swift)

Downloading in the background in iOS (Swift)

VyacheslavBakinskiy
Helpful | Level 6
Go to solution

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 solve 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()

                }

        }

    }

11 Replies 11

VyacheslavBakinskiy
Helpful | Level 6
Go to solution

I tested my application a little more and realized that this problem only occurs when the device is connected to a PC (Xcode), if I disconnect the device from the PC, then the download works fine.

Greg-DB
Dropbox Staff
Go to solution

I've been testing on a physical device. I've also had it connected to my computer though, to receive the output in Xcode. I'm not sure why that would interfere in your case, but I'm glad to hear it is working for you otherwise.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    VyacheslavBakinskiy Helpful | Level 6
What do Dropbox user levels mean?