1

I am using NSURLSession's dataTaskWithRequest to download data to my app.

Currently I show an activity view overlay on my view controller when my app downloads data from web server.

My user requested me to show a custom animation that tracks the execution completion of download process.

I know that dataTaskWithRequest is asynchronous and executes in background thread. I also know that I can NSURLSessionDelegate method

func URLSession(session: NSURLSession, task: NSURLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {}

to track progress of data being uploaded. So, I was wondering if there is any way to assign a progress view for download activity.

Any suggestions would be helpful. Thanks.

PK20
  • 1,066
  • 8
  • 19

1 Answers1

2

You should be able to use the delegate method

func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten writ: Int64, totalBytesExpectedToWrite exp: Int64)

which works pretty much the same way as didSendBodyData

It won't link the download to your progress view but it should be simple to set the progressView.progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite) inside that delegate method.

Moriya
  • 7,750
  • 3
  • 35
  • 53