【问题标题】:NSURLSessionDownloadTask Delegates Not FiringNSURLSessionDownloadTask 委托未触发
【发布时间】:2014-02-12 11:11:17
【问题描述】:

我正在玩 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);
}

在 .h 文件中:

#import <UIKit/UIKit.h>

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

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

@end

谁能建议如何解决?

【问题讨论】:

    标签: objective-c cocoa-touch ios7 nsurlconnection nsurlsession


    【解决方案1】:

    使用 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.
    }
    

    【讨论】:

      【解决方案2】:

      你已经有委托了,不如跳过completionHandler/block形式的任务创建,全押委托。

      快速浏览holy script 并没有告诉我任何关于指定完成处理程序是否会阻止委托方法被触发的权威信息,但这种关系有很多似乎是相互排斥的。

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

      【讨论】:

      • 解决了吗?如果有,是哪一部分?
      • 这是我的问题。在那里有完成块似乎取消了所有被调用的委托方法,而不仅仅是 didFinishDownloadingToURL 方法。
      猜你喜欢
      • 2015-11-27
      • 2023-04-11
      • 2018-11-27
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-18
      相关资源
      最近更新 更多