2

我想在用戶取消下載或發生錯誤後恢復下載。但是當我撥打cancelByProducingResumeData方法時,resumeDatanil。所以,我無法恢復下載。我確信下載鏈接可以恢復,因爲我們的PC客戶端可以恢復下載這個鏈接。 這是我的代碼。這裏是the full projectNSURLSession:調用cancelByProducingResumeData後無法獲取恢復數據

#import "ViewController.h" 

@interface ViewController() 
{ 
    NSURLSession *_session; 

} 

@property (weak, nonatomic) IBOutlet UIProgressView *progressView; 
@property NSURLSessionDownloadTask *netTask; 
@property NSData *resumeData; 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    if (_session == nil) { 
     NSURLSessionConfiguration *confi = [NSURLSessionConfiguration defaultSessionConfiguration]; 
     _session = [NSURLSession sessionWithConfiguration:confi delegate:self delegateQueue:[NSOperationQueue mainQueue]]; 
    } 
} 
- (IBAction)startAction:(id)sender { 
    [self start]; 
} 
- (IBAction)stopActon:(id)sender { 
    [self stop]; 
} 

- (void)stop { 
    __weak typeof(self) vc = self; 
    [self.netTask cancelByProducingResumeData:^(NSData * _Nullable resumeData) { 
     vc.resumeData = resumeData; 
     vc.netTask = nil; 
    }]; 
} 

- (void)start { 
    if (self.resumeData != nil) { 
     self.netTask = [_session downloadTaskWithResumeData:self.resumeData]; 
    } else { 
     NSURL *downlaodURL = [NSURL URLWithString:@"http://sdl24.yunpan.cn/share.php?method=Share.download&cqid=37ef0df7c8155bacf55c237bd433ddd8&dt=24.02b6cbb4148de503fe35ddab08dac35b&e=1459317290&fhash=41181b28ff97806ef8469842b4a5eabc330a0c60&fname=feistudy%2B%2B%25E8%25AF%25AD%25E8%25A8%2580%25E5%25AD%25A6%25E4%25B9%25A0%25E6%2596%25B9%25E6%25B3%2595%25E8%25AE%25BA%25E8%25BF%25B0&fsize=87624815&nid=14471440239484082&st=e08142ab7c935cdd15ecc8851c82e819&xqid=22309244"]; 
     self.netTask = [_session downloadTaskWithURL:downlaodURL]; 
    } 
    [self.netTask resume]; 
} 

- (NSString*)filePath 
{ 
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
    NSString *path = [doc stringByAppendingPathComponent:@"p.rmvb"]; 
    return path; 
} 

#pragma mark - NSURLSessionDownloadDelegate 
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask 
didFinishDownloadingToURL:(NSURL *)location 
{ 
    dispatch_async(dispatch_get_main_queue(), ^{ 

     NSFileManager *manager = [NSFileManager defaultManager]; 
     [manager moveItemAtPath:location.path toPath:[self filePath] error:nil]; 
     NSLog(@"locaton.path:%@", location.path); 
     NSLog(@"filePaht:%@",[self filePath]); 
    }); 

} 

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask 
     didWriteData:(int64_t)bytesWritten 
totalBytesWritten:(int64_t)totalBytesWritten 
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite 
{ 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     double progress = totalBytesWritten/(double)totalBytesExpectedToWrite; 
     NSLog(@"progress:%f",progress); 
     self.progressView.progress = progress; 
    }); 

} 

#pragma mark - NSURLSessionTaskDelegate 
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task 
didCompleteWithError:(nullable NSError *)error 
{ 
    if (error) { 

     NSData *resumeData = error.userInfo[NSURLSessionDownloadTaskResumeData]; 
     self.resumeData = resumeData; 
    } 



} 

@end 
+0

當您恢復數據下載? – NSPratik

回答

1

對於什麼時候可以獲得簡歷數據,有一長串的要求。其中包括:

  • 響應必須包含ETag或Last-Modified標頭。
  • 我認爲響應還必須包含Accept-Ranges:bytes標頭
  • 臨時文件必須仍然存在(磁盤空間不低)。
  • 它必須是HTTP或HTTPS請求。
  • Last-Modified或ETag標頭必須指示該文件自上次請求以來未發生更改。

可能還有其他我忘記的要求,例如, HTTP/1.1。