i create 1 application download file url size.i have 1 button , 1 progress view in page . want when click on button downloaded file , show me download's status in progress view.
i can download file code dont know how use progress view code:
- (ibaction)download:(id)sender { dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ nslog(@"downloading started"); nsstring *urltodownload = @"http://192.168.1.100/emma/adele%20-%20someone%20like%20you%20(mtv%20video%20music%20awards%202011)%20hd%20live.mkv"; nsurl *url = [nsurl urlwithstring:urltodownload]; nsdata *urldata = [nsdata datawithcontentsofurl:url]; if ( urldata ) { nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *filepath = [nsstring stringwithformat:@"%@/%@", documentsdirectory,@"filename.mkv"]; dispatch_async(dispatch_get_main_queue(), ^{ [urldata writetofile:filepath atomically:yes]; nslog(@"file saved !"); }); x = 1; nslog(@"x : %d",x); } }); } please guide me it.
define nstimer *timer in .h file , set progressview in .xib
- (ibaction)download:(id)sender { dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ nslog(@"downloading started"); nsstring *urltodownload = @"http://192.168.1.100/emma/adele%20-%20someone%20like%20you%20(mtv%20video%20music%20awards%202011)%20hd%20live.mkv"; nsurl *url = [nsurl urlwithstring:urltodownload]; nsdata *urldata = [nsdata datawithcontentsofurl:url]; if ( urldata ) { nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *filepath = [nsstring stringwithformat:@"%@/%@", documentsdirectory,@"filename.mkv"]; timer = [nstimer timerwithtimeinterval:0.5 target:self selector:@selector(updateprogressview) userinfo:nil repeats:yes]; [timer fire]; dispatch_async(dispatch_get_main_queue(), ^{ [urldata writetofile:filepath atomically:yes]; nslog(@"file saved !"); }); x = 1; nslog(@"x : %d",x); } }); } and add method
- (void) updateprogressview{ nsstring *urltodownload = @"http://192.168.1.100/emma/adele%20-%20someone%20like%20you%20(mtv%20video%20music%20awards%202011)%20hd%20live.mkv"; nsurl *url = [nsurl urlwithstring:urltodownload]; nsdata *urldata = [nsdata datawithcontentsofurl:url]; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *filepath = [nsstring stringwithformat:@"%@/%@", documentsdirectory,@"filename.mkv"]; nsdata *writtendata = [nsdata datawithcontentsoffile:filepath]; float progress = [writtendata length]/(float)[urldata length]; [progressview setprogress:progress]; if (progress == 1.0){ [timer invalidate]; } }
Comments
Post a Comment