Download Files using URLSessionDownloadTask in Swift

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

 

. . .

Comment

Add Your Comment

Be the first to comment.

css.php
Hire Us!
Brief us about your requirements and we'll get back to you.
Woo! Hooy!
We have just recieved your project brief and our expert will contact you shortly.
Send Again
Close