How to download a file by using NSURLConnection?
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 o开发者_运维问答r website reference (I mean the example) for me? Thank you very much.
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
精彩评论