微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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”有效

原文地址:https://www.jb51.cc/c/116971.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐