ios - Cancellable file download -


i have code this:

- (void)downloadfile:(void (^)(bool success))callback {     dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_high, 0), ^{             nsurl *url = [nsurl urlwithstring:@"http://stackoverflow.com/largefile.bin"];             nsdata *data = [nsdata datawithcontentsofurl:url];             callback(yes);     }); } 

also have progress dialog created , shown before calling method , hidden after callback. need somehow able cancel file download. how can this?

answer based on @fogmeister comment.

downloader.h:

@interface downloader : nsobject<nsurlconnectiondatadelegate>  - (void) download:(nsurl *)url; - (void) cancel;  @end 

downloader.m:

@implementation downloader {     nsmutabledata * receiveddata;     nsurlconnection * urlconnection; }  - (void) download:(nsurl *)url {     nsurlrequest *therequest = [nsurlrequest requestwithurl:url cachepolicy:nsurlrequestreloadignoringlocalcachedata timeoutinterval:60];     receiveddata = [[nsmutabledata alloc] initwithlength:0];      urlconnection = [[nsurlconnection alloc] initwithrequest:therequest delegate:self startimmediately:yes]; }  - (void) cancel {     if (urlconnection != nil) {         [urlconnection cancel];         urlconnection = nil;     } }  - (void) connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error {     urlconnection = nil; }  - (void) connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data {     [receiveddata appenddata:data]; }  - (void) connectiondidfinishloading:(nsurlconnection *)connection {     urlconnection = nil;     // process receiveddata } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -