objective-c – AFNetworking暂停/恢复下载大文件

我需要使用我的iPad应用程序下载大型.zip文件(最大800 MB).
如果取消下载或应用程序在后台,我想再次恢复下载.
operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:YES];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,id responSEObject){

// unzip the .zip


}failure:^(AFHTTPRequestOperation *operation,NSError *error){

//Handle the error

}];


[operation setDownloadProgressBlock:^(NSInteger bytesWritten,long long totalBytesWritten,long long totalBytesExpectedToWrite) {

//update the progressView

}];

[operation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{

// What I have to do here?

}];

[operation start];
[operation waitUntilFinished];


-(void)applicationWillResignActive:(UIApplication *)application{

// What I have to do here?

}

谢谢你的帮助.

解决方法

您可以使用AFDownloadRequestOperation执行此操作.
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"....zip"]];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    Nsstring *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"....zip"];
    AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:path shouldResume:YES];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,id responSEObject) {
        NSLog(@"Successfully downloaded file to %@",path);
    } failure:^(AFHTTPRequestOperation *operation,NSError *error) {
        NSLog(@"Error: %@",error);
    }];
    [operation setProgressiveDownloadProgressBlock:^(NSInteger bytesRead,long long totalBytesRead,long long totalBytesExpected,long long totalBytesReadForFile,long long totalBytesExpectedToReadForFile) {
        NSLog(@"Operation%i: bytesRead: %d",1,bytesRead);
        NSLog(@"Operation%i: totalBytesRead: %lld",totalBytesRead);
        NSLog(@"Operation%i: totalBytesExpected: %lld",totalBytesExpected);
        NSLog(@"Operation%i: totalBytesReadForFile: %lld",totalBytesReadForFile);
        NSLog(@"Operation%i: totalBytesExpectedToReadForFile: %lld",totalBytesExpectedToReadForFile);
    }];
    [operations addobject:operation];

重新启动应用程序并生成具有相同URL的请求后,它将继续下载.“shouldResume:YES”有效

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...