NSURLSessionDownloadTask代表未触发

Guy*_*ood 2 cocoa-touch objective-c nsurlconnection ios7 nsurlsession

我正在玩NSURLSession的教程.我可以成功下载图像,但下载进度和下载完成的代表不会触发.这是代码:

- (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);
}
Run Code Online (Sandbox Code Playgroud)

在.h文件中:

#import <UIKit/UIKit.h>

@interface SGGViewController : UIViewController <NSURLSessionDelegate> {
    IBOutlet UIImageView * imageView;
}

@property (nonatomic, strong) IBOutlet UIImageView * imageView;

@end
Run Code Online (Sandbox Code Playgroud)

任何人都可以建议如何解决?

Man*_*amp 7

使用NSUrlRequest现在代理人将调用.希望这会奏效

 - (void)viewDidLoad
{
    [super viewDidLoad];

    NSURLSessionDownloadTask *downloadTask =nil;
    NSString * imageUrl = @"http://fc05.deviantart.net/fs71/i/2012/180/8/f/ios_6_logo_psd___png_by_theintenseplayer-d55eje9.png";
    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession * session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:[NSOperationQueue mainQueue]] ;
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]];

    downloadTask = [session downloadTaskWithRequest:request];
    [downloadTask resume ];

    /*
    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.
}
Run Code Online (Sandbox Code Playgroud)


Cla*_*ges 5

你已经有了一个委托,所以你也可以跳过completionHandler任务创建的/ block形式,然后全押在委托上.

快速浏览一下神圣的脚本并没有告诉我任何关于指定一个完成处理程序是否会阻止委托方法被触发的权威,但是有很多关于这种似乎相互排斥的关系.

如果你还没有,我会说你应该加入–URLSession:task:didCompleteWithError:你的代表.它可能会捕获纯粹的下载委托方法可能会遗漏的问题.