iphone - Know the download progress for update an UIprogressView -


my app downloads video internet , saves in iphone.

i'm using code

nsmutableurlrequest *request = [[[nsmutableurlrequest alloc] init] autorelease];  [request sethttpmethod:@"get"]; nserror *error; nsurlresponse *response;   nsstring *documentfolderpath = [nshomedirectory() stringbyappendingpathcomponent:@"documents"]; nsfilemanager *filemanager = [nsfilemanager defaultmanager]; nsstring *videosfolderpath = [documentfolderpath stringbyappendingpathcomponent:@"videos"];   //check if videos folder exists, if not, create it!!! bool isdir; if (([filemanager fileexistsatpath:videosfolderpath isdirectory:&isdir] && isdir) == false) {     [[nsfilemanager defaultmanager] createdirectoryatpath:videosfolderpath withintermediatedirectories:yes attributes:nil error:nil]; }  nsstring *filepath = [videosfolderpath stringbyappendingpathcomponent:@"name.mp4"];  if ([filemanager fileexistsatpath:filepath ] == yes) {     nslog (@"file exists"); } else {     nslog (@"file not found");     nsdata *urldata;     nsstring *downloadpath = @"http://www.mywebsite.com/name.mp4";     [request seturl:[nsurl urlwithstring:downloadpath]];     urldata = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error];      bool written = [urldata writetofile:filepath atomically:no];     if (written)         nslog(@"saved file: %@", filepath);     else {nslog(@"problem");} } 

all works fine, want add progress view, indicate status of download.

the problem (i think i'm not sure) nsurlconnection hasn't delegate, methods like

- (void)connection:(nsurlconnection *)theconnection didreceivedata:(nsdata *)data  

or

-(void)connection:(nsurlconnection *)connection didsendbodydata:(nsinteger)byteswritten totalbyteswritten:(nsinteger)totalbyteswritten totalbytesexpectedtowrite:(nsinteger)totalbytesexpectedtowrite{ 

are never called.

i tried use

urldata = [[nsurlconnection alloc] initwithrequest:request delegate:self]; 

but app crashes when try write file

[urldata writetofile:filepath atomically:no]; 

what can do? thanks

what doing sending "synchronous" request, meaning thread downloading http response hang until data has been fetched. never on ui thread, when not want display indicator of download's progress. suggest using nsurlconnection class, setting it's delegate, , responding delegate methods. small tutorial can found @ http://snippets.aktagon.com/snippets/350-how-to-make-asynchronous-http-requests-with-nsurlconnection.

once connection's delegate, can content length when connection receives response:

- (void)connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response {     nshttpurlresponse * httpresponse = (nshttpurlresponse *)response;        contentsize = [httpresponse expectedcontentlength]; } 

then, calculate total progress of download whenever new data arrives, this:

- (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data {     // download data our global nsmutabledata object contains     // fetched http data.     [downloaddata appenddata:data];     float progress = (float)[downloaddata length] / (float)contentsize;     [progressview setvalue:progress]; } 

update:

another example on how async http foundation: http://codewithchris.com/tutorial-how-to-use-ios-nsurlconnection-by-example/


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -