OGeek|极客世界-中国程序员成长平台

标题: ios - NSURLSession:调用cancelByProducingResumeData后无法获取恢复数据 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 15:41
标题: ios - NSURLSession:调用cancelByProducingResumeData后无法获取恢复数据

我想在用户取消下载或发生错误后继续下载。但是当我调用 cancelByProducingResumeData 方法时,resumeDatanil。所以,我无法继续下载。我确定可以恢复下载链接,因为我们的PC客户端可以恢复下载该链接。 这是我的代码。这里是 the full project .

#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)startActionid)sender {
    [self start];
}
- (IBAction)stopActonid)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)URLSessionNSURLSession *)session downloadTaskNSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURLNSURL *)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)URLSessionNSURLSession *)session downloadTaskNSURLSessionDownloadTask *)downloadTask
      didWriteDataint64_t)bytesWritten
 totalBytesWrittenint64_t)totalBytesWritten
totalBytesExpectedToWriteint64_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



Best Answer-推荐答案


关于何时可以获取简历数据的要求很长。除其他事项外:

我可能忘记了其他要求,例如HTTP/1.1。

关于ios - NSURLSession:调用cancelByProducingResumeData后无法获取恢复数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36257103/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) Powered by Discuz! X3.4