How to download a file by using NSURLConnection?

前端 未结 1 1709
离开以前
离开以前 2021-01-26 00:23

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 Appl

1条回答
  •  天涯浪人
    2021-01-26 01:06

    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

    0 讨论(0)
提交回复
热议问题