NSURLSessionDownloadTask Delegates Not Firing
cocoa-touch, ios7, nsurlconnection, nsurlsession, objective-c
Solution
You already have a delegate, so you may as well skip the the `completionHandler`/block form of task creation, and go all-in on the delegate.
A quick glance at the holy script didn't tell me anything authoritative about whether specifying a completion handler will prevent the delegate methods from being fired, but there is a lot about that relationship that seems mutually exclusive.
If you haven't already, I'd say you should add `–URLSession:task:didCompleteWithError:` to your delegate. It might capture problems the purely download delegate methods might miss.
Problem
I'm playing around with a tutorial for NSURLSession. I can successfully download an image, however the delegates for download progress and download finished are not triggering. Here is the code : ``` - (void)viewDidLoad { [super viewDidLoad]; NSString * imageUrl = @"http://ichef.bbci.co.uk/naturelibrary/images/ic/credit/640x395/r/ro/rock_pigeon/rock_pigeon_1.jpg"; NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession * session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil]; //Download image. NSURLSessionDownloadTask * getImageTask = [session downloadTaskWithURL:[NSURL URLWithString:imageUrl] completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) { if (error) { NSLog(@"Error sadly for you is %@", [error localizedDescription]); } UIImage * downloadedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]]; dispatch_async(dispatch_get_main_queue(), ^ { self.imageView.image = downloadedImage; }); }]; [getImageTask resume]; // Do any additional setup after loading the view, typically from a nib. } -(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { NSLog(@"Temporary File :%@\n", location); NSError *err = nil; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSURL *docsDirURL = [NSURL fileURLWithPath:[docsDir stringByAppendingPathComponent:@"out1.zip"]]; if ([fileManager moveItemAtURL:location toURL:docsDirURL error: &err]) { NSLog(@"File is saved to =%@",docsDir); } else { NSLog(@"failed to move: %@",[err userInfo]); } } -(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite { //You can get progress here NSLog(@"Received: %lld bytes (Downloaded: %lld bytes) Expected: %lld bytes.\n", bytesWritten, totalBytesWritten, totalBytesExpectedToWrite); } ``` And in the .h file : ``` #import <UIKit/UIKit.h> @interface SGGViewController : UIViewController <NSURLSessionDelegate> { IBOutlet UIImageView * imageView; } @property (nonatomic, strong) IBOutlet UIImageView * imageView; @end ``` Can anyone suggest how to fix ?