Vincent Tourraine
Vincent Tourraine

Reputation: 801

How to get the size of a remote file with iPhone SDK?

My iPhone application downloads large files from the internet (videos, for instance).

I'd like to display a progress bar, so I need to get the size of the remote file I'm trying to download (before downloading it, of course).

I've searched, again and again, but nothing is working. Is there any way to do that ?

Thank you by advance.

Upvotes: 2

Views: 2937

Answers (3)

Vincent Tourraine
Vincent Tourraine

Reputation: 801

Ok, I finally get my answer.

The NSURLConnection calls its delegate with

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;

Well, we need to look inside the NSURLResponse, to find the expectedContentLength

- (long long)expectedContentLength

Return Value The receiver’s expected content length, or NSURLResponseUnknownLength if the length can’t be determined.

Upvotes: 2

invalidname
invalidname

Reputation: 3185

If you're using the URL Loading System (NSURLRequest, NSURLConnection, etc.), then look to see if there's a value for the NSURLConnection's expectedContentLength. Note that it might be NSURLResponseUnknownLength.

Upvotes: 4

Herms
Herms

Reputation: 38868

You'll need some mechanism for the server to report the size to you.

I would expect most servers to provide the content-length header entry in the response. That should tell you how big the file is. I would assume whatever class you're using to do the download would expose that information to you.

Upvotes: 1

Related Questions