未调用 NSURLSession 委托

NSURLSession delegate not called

我想知道为什么我的委托方法 -(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location 没有被调用。有我的代码:

- (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);    
}

我还想补充一点,我的方法:-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite 工作正常(它显示输出我得到了多少字节)。

你有没有尝试实施

-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error

查看是否返回了一些错误?