在我的项目中,我启动NSURLSessionDataTask
来检查请求的标头,如果它们是正确的,我会将请求转为下载,或者我只是丢弃它:
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
if (XYZ) {
completionHandler(NSURLSessionResponseCancel);
}
else {
completionHandler(NSURLSessionResponseBecomeDownload);
}
}
}
但是,我遇到了一个问题:一旦会话变为NSURLSessionDownloadTask
,有时没有任何事情发生,- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
根本没有被调用,- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
也是。
这里发生了什么?这有意义吗?!
---编辑---
显然这解决了这个问题?
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didBecomeDownloadTask:(NSURLSessionDownloadTask *)downloadTask
{
// Resume the Download Task manually because apparently iOS does not do it automatically?!
[downloadTask resume];
}