2

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?

Satish Mavani
  • 4,897
  • 2
  • 20
  • 30

1 Answers1

2

-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"];
Mohmmad S
  • 5,001
  • 4
  • 18
  • 50
  • Thanks @Tobi, I have tried by adding fetch req parameter Accept-Encoding but it didn't work for me. Let me know if you can think of any other solution. – Satish Mavani Oct 08 '18 at 11:50
  • The only possible fix is to change the server side code to send a Content-Length header. The client can't know the length unless the server tells it the length. – dgatwood Oct 08 '18 at 20:37