Download Files using URLSessionDownloadTask in Swift

Updated 6 July 2023


Download Files using URLSessionDownloadTask

Here we are going to discuss how to download large files and save partial data even when the app is terminated and also updating the view when is in the background.

Creating an object of  URLSession

Create an object of  URLSession which you have to config the identifier using URLSessionConfiguration after make isDiscretionary true so that we will enable downloading with performance in the background state.

After that pass URL from which you want to download the using downloadTask method of URL session class

then file downloading will start to track the download data likes totalBytesExpectedToWrite, totalBytesWritten from that you can calculate how much data is downloaded with this delegate method

After when your file is downloaded you can save in directory of your app.

Suppose user put your application in background mode then handleEventsForBackgroundURLSession method call of the app delegate class

And downloading is completed in background state then you have to update the view in the main thread


. . .

Leave a Comment

Your email address will not be published. Required fields are marked*

Be the first to comment.

Start a Project

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home