【发布时间】:2014-06-28 10:08:31
【问题描述】:
我遇到了一个奇怪的问题。我使用 NSURLSession 和 NSURLSessionDownloadTask 从 Internet 加载文件。这是代码
NSURLSessionConfiguration *sessionConfiguration =
[NSURLSessionConfiguration backgroundSessionConfiguration:kSessionId];
self.session = [NSURLSession sessionWithConfiguration:sessionConfiguration
delegate:self
delegateQueue:[NSOperationQueue new]];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDownloadTask *downloadTask = [self.session downloadTaskWithRequest:request];
[downloadTask resume];
我的班级被声明为NSURLSessionDownloadDelegate,我得到了很好的回调。但是当系统调用委托方法时
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
NSLog(@"totalBytesExpectedToWrite: %lld", totalBytesExpectedToWrite);
NSLog(@"%lld", totalBytesWritten);
}
totalBytesExpectedToWrite 总是等于-1,我无法向用户显示进度,因为我不知道下载文件的大小。
你能提示我哪里出错了吗?
【问题讨论】:
-
这可能是一个服务器问题,它没有正确发送
Content-Length标头。如果您在浏览器中使用相同的 URL,您的浏览器是否显示正确的进度?
标签: ios nsurlsession