使用NSURLSession如何在取消downloadtask时获取接收数据或临时文件位置

时间:2014-12-04 05:25:50

标签: ios nsurlsession nsurlsessiondownloadtask

我正在使用NSURLSession下载多个文件,我想在取消下载任务时保存下载的数据,并且在恢复时想要开始下载其余数据。 我的

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
    didReceiveData:(NSData *)data

没有被调用。

下载我的代码:

sessionConfig =[NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"Download Manager"];

sessionConfig.allowsCellularAccess=!_UseOnlyWiFi;

sessionConfig.HTTPMaximumConnectionsPerHost=1;

queue.maxConcurrentOperationCount=1;

session =[NSURLSession sessionWithConfiguration:sessionConfig
                                                         delegate:self
                                                    delegateQueue:[NSOperationQueue mainQueue]];


NSURLSessionDownloadTask *downloadTask=[session downloadTaskWithURL:dwurl ];

[downloadTask resume];

2 个答案:

答案 0 :(得分:0)

`you have implemented datatask delegate but created download task implement this`

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
                                       didWriteData:(int64_t)bytesWritten
                                  totalBytesWritten:(int64_t)totalBytesWritten
                          totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite;
instead of
(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data 

答案 1 :(得分:0)

也许你可以尝试使用

cancelByProducingResumeData:

它在Apple documentation上说:

Cancels a download and calls a callback with resume data for later use.