NSURLConnectionDownloadDelegate expectedTotalBytes Zero in iOS 6

I have a problem with

connection:didWriteData:totalBytesWritten:expectedTotalBytes: 

NSURLConnectionDownloadDelegate method under iOS 6.

In iOS 5 simulator, it works fine, giving me the correct expected TotalBytes value.

In iOS 6 Simulator, the expected TotalBytes always returns 0. The value of totalBytesWritten is still valid.

The same request, the same URL, just the OS version is different.

Has anyone encountered a similar problem or had any idea what might cause this?

Cheers Kim

+4
source share
1 answer

I just spent a lot of time studying the same problem. It turned out that in iOS6, the request was first made with HEAD to examine the headers, which is not abnormal.

However, it seems that the response headers from the actual GET request are ignored. As a result, if your server did not support HEAD or did not return 0 content lengths for a HEAD request for a given URL, iOS NSURLConnection will use the wrong information.

My problem was that my user server did not support HEAD requests for uploaded files and instead returned 405 (HTTP Error 405 is not allowed), which in its own response contained the length of the content that iOS6 then used to return the expected TotalBytes, and not correct from the GET answer.

To fix my problem, I first turned on HEAD for file upload requests, and then returned the correct content length. Confirmed by:

 curl -v -I http://url ... < Content-Length: 23493947 Content-Length: 23493947 

Not sure if this is a bug in iOS6 or just better compliance with HTTP standards. Hope that helps others.

+7
source

All Articles