How to integrate NSURLConnection with UIProgressView? How to integrate NSURLConnection with UIProgressView? ios ios

How to integrate NSURLConnection with UIProgressView?

Not sure what I'm missing here, but your filesize being -1 seems to be your problem. The API docs clearly state that expectedContentLength may not be available and that NSURLResponseUnknownLength is returned in these cases. NSURLResponseUnknownLength is defined as:

#define NSURLResponseUnknownLength ((long long)-1)

In these cases, you cannot get an accurate progress. You'll need to handle this and display an indeterminate progress meter of some sort.

I think you are printing out the file size wrong. If self.filesize is indeed an NSNumber, you print it using the %@ format, because it is an object, not a primitive:

NSLog(@"filesize: %@", self.filesize);

By using the %d, your are just printing the pointer value of self.filesize. To print out the actual long long value, use %lli (%d is only for 32-bit values):

NSLog(@"filesize: %lli", [self.filesize longLongValue]);

So your self.filesize is actually -1 and the division is correct.

In your code, filesize appears to be an NSNumber object (!). So

NSLog(@"filesize: %d", self.filesize);


NSLog(@"content-length: %d bytes", self.filesize);

will likely report something like the address (id) of that object (or something else). This is the

filesize: 4687472

you see. As pointed out by others, the file size returned by the response is indeed -1, i.e.,


i.e., the server did not return the file size.