我需要使用我的iPad应用程序下载大型.zip文件(最大800 MB).
如果取消下载或应用程序在后台,我想再次恢复下载. @H_301_3@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? }
如果取消下载或应用程序在后台,我想再次恢复下载. @H_301_3@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执行此操作.
@H_301_3@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”有效