Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURLSession delegate and completionHandler

I am using NSURLSession + NSURLDownloadTask with completionHandler:

[session downloadTaskWithURL:downloadURL completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error)

it works nice.

But when I want to track download in a progressBar I am running into problems. I am trying to use NSURLSessionDownloadDelegate to track loading. With the upper completionHandler it is never called?!

Using:

 NSURLSessionDownloadTask *downloadTask =
[session downloadTaskWithURL:downloadURL];

will call the delegates.

Is there any way to use completionHandler AND delegates?
Or any other way to track download while using completionHandler?

like image 414
Yedy Avatar asked Mar 04 '14 14:03

Yedy


1 Answers

To employ the didWriteData method of the NSURLSessionDownloadDelegate, you sadly have to use the rendition of downloadTaskWithURL without the completionHandler and then implement your own URLSession:downloadTask:didFinishDownloadingToURL: to perform those actions you otherwise would have done in the completion handler.

This is a little annoying (especially since the NSURLSessionDownloadDelegate is set at the session object), but it's how it works.

like image 126
Rob Avatar answered Nov 15 '22 00:11

Rob