views:

300

answers:

2

Hello,

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.

+4  A: 

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).

Dave DeLong
A: 

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

There is: NSURLDownload.

Peter Hosey