开发者

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 开发者_Go百科if theres an "official" method to download file in cocoa because up until now I just used curl with NSTask. Thanks for any replies.


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


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

There is: NSURLDownload.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜