nosedive25
nosedive25

Reputation: 2435

Download file with progress bar in cocoa?

I need to have a progress bar that responds to the percent complete of a download in cocoa. I think this might use things like NSProgressindicator and possibly NSTask. I'm not sure if theres an "official" method to download file in cocoa because up until now I just used curl with NSTask. Thanks for any replies.

Upvotes: 0

Views: 3414

Answers (2)

Peter Hosey
Peter Hosey

Reputation: 96323

I'm not sure if theres an "official" method to download file in cocoa …

There is: NSURLDownload.

Upvotes: 3

Dave DeLong
Dave DeLong

Reputation: 243146

Use NSURLConnection. It has a delegate property, which means it will tell you periodically that it has received more data (which you then have to save to a file yourself). However, it also tells you when it has started retrieving the file, and if you're downloading a file over HTTP (which I think is a safe assumption), it will most likely have a Content-Length: HTTP header, so you can know what to set as the maxValue of your NSProgressIndicator. Then just incrementBy: the size of the data your connection has told you it just received.

If for some reason you get a chunked HTTP transfer, then just set the progress bar to be indeterminate, since that's the whole point of a chunked transfer (that you don't know how big it's going to be).

Upvotes: 7

Related Questions