ios - NSURLSession:调用cancelByProducingResumeData后无法获取恢复数据
<p><p>我想在用户取消下载或发生错误后继续下载。但是当我调用 <code>cancelByProducingResumeData</code> 方法时,<code>resumeData</code> 是 <code>nil</code>。所以,我无法继续下载。我确定可以恢复下载链接,因为我们的PC客户端可以恢复下载该链接。
这是我的代码。这里是 <a href="https://drive.google.com/file/d/0B0Ygk57VnimYeEdtdnEyVXU2U3M/view?usp=sharing" rel="noreferrer noopener nofollow">the full project</a> .</p>
<pre><code>#import "ViewController.h"
@interface ViewController ()
{
NSURLSession *_session;
}
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;
@property NSURLSessionDownloadTask *netTask;
@property NSData *resumeData;
@end
@implementation ViewController
- (void)viewDidLoad {
;
// Do any additional setup after loading the view, typically from a nib.
if (_session == nil) {
NSURLSessionConfiguration *confi = ;
_session = ];
}
}
- (IBAction)startAction:(id)sender {
;
}
- (IBAction)stopActon:(id)sender {
;
}
- (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 = ;
} else {
NSURL *downlaodURL = ;
self.netTask = ;
}
;
}
- (NSString*)filePath
{
NSString *doc = ;
NSString *path = ;
return path;
}
#pragma mark - NSURLSessionDownloadDelegate
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location
{
dispatch_async(dispatch_get_main_queue(), ^{
NSFileManager *manager = ;
error:nil];
NSLog(@"locaton.path:%@", location.path);
NSLog(@"filePaht:%@",);
});
}
- (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;
self.resumeData = resumeData;
}
}
@end
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>关于何时可以获取简历数据的要求很长。除其他事项外:</p>
<ul>
<li>响应必须包含 ETag 或 Last-Modifiedheader 。</li>
<li>我认为响应还必须包含一个 Accept-Ranges: bytesheader </li>
<li>临时文件必须仍然存在(磁盘空间不低)。</li>
<li>必须是 HTTP 或 HTTPS 请求。</li>
<li>Last-Modified 或 ETagheader 必须表明文件自上次请求以来未更改。</li>
</ul>
<p>我可能忘记了其他要求,例如HTTP/1.1。</p></p>
<p style="font-size: 20px;">关于ios - NSURLSession:调用cancelByProducingResumeData后无法获取恢复数据,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/36257103/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/36257103/
</a>
</p>
页:
[1]