问题
I have created URLSession for downloading a file, File is being downloaded correctly no problem with that.
I want to show the percentage count of remain bytes, but the delegate function:
-(void) URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
and its parameter totalBytesExpectedToWrite
always returns -1.
All thing was working fine before few days, there is no change with code but it suddenly stopped sending Expected bytes.
My request code is like:
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
NSMutableURLRequest*request = [NSMutableURLRequest requestWithURL:fileUrl];
NSDictionary*param = [[NSDictionary alloc]initWithObjectsAndKeys:@"",@"Accept-Encoding", nil];
[request setAllHTTPHeaderFields:param];
NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request];
[downloadTask resume];
is there any change with the api which i am missing? OR any other way around?
回答1:
-1
is NSURLSessionTransferSizeUnknown
, which means that the http server did not provide a "Content-Length" header (and the data is sent using "Transfer-Encoding: chunked").
There is probably not much that you can do. You could try if the workaround from https://stackoverflow.com/a/12599242/1187415 works in your case as well:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:anURL];
[request addValue:@"" forHTTPHeaderField:@"Accept-Encoding"];
来源:https://stackoverflow.com/questions/52698264/urlsession-delegate-function-totalbytesexpectedtowrite-always-return-1