I wonder why my delegate method -(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
not called. There is my code:
- (void)viewDidLoad {
[self createSessionWithDelegate];
[super viewDidLoad];
}
- (void)createSessionWithDelegate {
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
NSURLSessionDownloadTask *task = [session downloadTaskWithURL:[NSURL URLWithString:@"http://example.com/my-expected-image.jpg"]];
[task resume];
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
UIImage *downloadedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]];
if (downloadedImage){
NSLog(@"Got image!");
}
// Perform UI changes in main thread
dispatch_async(dispatch_get_main_queue(), ^{
self.myImageView.image = downloadedImage;
});
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
NSLog(@"%f / %f", (double)totalBytesWritten, (double)totalBytesExpectedToWrite);
}
Also I want to add, that my method : -(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
works correctly (it shows output how much bytes I got).
UIViewController <NSURLSessionDelegate>
? – wigging