Set progress bar for downloading NSData

前端 未结 3 509
失恋的感觉
失恋的感觉 2021-02-09 14:21
NSURL *url = [NSURL URLWithString:@\"http://i0.kym-cdn.com/entries/icons/original/000/005/545/OpoQQ.jpg?1302279173\"]; 
NSData *data = [NSData dataWithContentsOfURL:url]         


        
3条回答
  •  时光说笑
    2021-02-09 14:51

    You can't get progress call backs by using that method.

    You need to use an NSURLConnection and NSURLConnectionDataDelegate.

    The NSURLConnection then runs asynchronously and will send callbacks to its delegate.

    The main ones to look at are...

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
    

    and

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection;
    

    These are all used for getting the connection to do what you're already doing.

    EDIT

    Actually, see Marc's answer below. It is correct.

提交回复
热议问题