iphone - Objective-C Check for downloaded file size -
i'm creating app downloads .zip file s3 server. works fine. want able interrupt current download. if save current size (bytes) of file, able send new request range header other part of file.
problem lies in fact cannot determine size of 'already' downloaded content, because can see file in directory when download completed. if interrupt, there isn't partial file saved.
at time use following code this:
-(void) downloadfile:(nsmutablearray*)paramarray withdict:(nsmutabledictionary*)options { nslog(@"download thread started"); nsstring * sourceurl = [paramarray objectatindex:0]; nsstring * filename = [paramarray objectatindex:1]; nsautoreleasepool *pool = [[nsautoreleasepool alloc] init]; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *newfilepath = [documentsdirectory stringbyappendingstring:filename]; nserror *error=[[[nserror alloc]init] autorelease]; nsurlconnection *fileurl = [nsdata datawithcontentsofurl: [nsurl urlwithstring:sourceurl]]; bool response = [fileurl writetofile:newfilepath options:nsdatawritingfileprotectionnone error:&error]; if (response == true) { nslog(@"download completed"); [self performselectoronmainthread:@selector(downloadcomplete:withdict:) withobject:paramarray waituntildone:yes]; } else { nslog(@"something went wrong while downloading file."); nsstring *callback = [nsstring stringwithformat:@"downloadinterrupted('%@');",filename]; [webview stringbyevaluatingjavascriptfromstring:callback]; } [pool drain]; }
asihttp isn't option because there issues phonegap i'm using.
a better idea download file asynchronously. has several advantages: important 1 user interface stays responsive. user can go on using application while downloading , waiting data. if data downloading absolutely essential application, display sort of loading indicator.
you can start asynchronous download via
nsurlrequest *request = [nsurlrequest requestwithurl:[nsurl urlwithstring:sourceurl]]; nsurlconnection *connection = [nsurlconnection connectionwithrequest:request delegate:self];
now, how downloades data in nsdata
object? implement following delegate methods self
:
-connection:didreceivedata: -connection:didfailwitherror: -connectiondidfinishloading:
the idea notified whenever data drops in through connection or important else happens (success or failure exmple). going declare temporary nsmutabledata
object instance variable (say downloaddata
) , write until download complete. not forget initialize empty object , declare property well!
-connection:didreceivedata:
called whenever sort of data (that is, part of downloaded file) arrives. going append temporary object this:
-(void) connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data { [self.downloaddata appenddata:data]; }
once download has finished (successfully), next delegate method called:
-(void) connectiondidfinishloading:(nsurlconnection *)connection { //do whatever need data in self.downloaddata }
if downloads fails, -connection:didfailwitherror:
called. can save temporary object, size , resume download later. [self.downloaddata length];
gets size in bytes of data in object.
Comments
Post a Comment