How to download a file by using NSURLConnection?

℡╲_俬逩灬. 提交于 2019-12-02 19:39:29

问题


I want to ask a question about the objective C. I want to download a .vcf file from a server (CardDav server) in iPhone application. After I read the API and library of the Apple Developer, I found that I should use the NSURLConnection Class. However, I don't know how to start the program.

Therefore, I want to ask can anyone give me some tutorial or website reference (I mean the example) for me? Thank you very much.


回答1:


You can use this method:

- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate startImmediately:(BOOL)startImmediately

More here and the delegate:

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

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

The delegate is an object that can respond to 2 above methods to receive the data. If you want to do asynchronous, you have to use delegate. If you don't, you can just do:

+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error

[More here][2]

Or you can just do like this for some data:

+ (id)dataWithContentsOfURL:(NSURL *)aURL

More here



来源:https://stackoverflow.com/questions/3464252/how-to-download-a-file-by-using-nsurlconnection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!