The layout is as follows:
Basic Drag Properties:
#import "ViewController.h" #import "AFNetworking.h" @interface Viewcontroller () @property (weak, nonatomic) Iboutlet UILabel *progresslabel; @property (weak, nonatomic) Iboutlet Uiprogressview *progressview; @property (nonatomic, Strong) Afhttprequestoperation *operation, @end @implementation Viewcontroller
Call:
-(void) viewdidload{ [Super Viewdidload]; NSString *cachepath = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES). LastObject; NSString *txtpath = [CachePath stringbyappendingpathcomponent:@ "Mvtemp/mv.txt"]; Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager]; if ([FileManager Fileexistsatpath:txtpath]) { self.progressView.progress = [[NSString stringwithcontentsoffile: Txtpath encoding:nsutf8stringencoding Error:nil] floatvalue]; Self.progressLabel.text = [NSString stringwithformat:@ "%.2f%%", _progressview.progress *]; } else { self.progressView.progress = 0; Self.progressLabel.text = @ "0%"; } NSLog (@ "%@", Nshomedirectory ()); }
Click events:
-(Ibaction) Startorcanceldownload: (UIButton *) sender{if ([sender.currenttitle isequaltostring:@ "start Download]") {[Send Er settitle:@ "pause download" forstate:uicontrolstatenormal]; NSString *cachepath = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES). LastObject; NSString *filepath = [CachePath stringbyappendingpathcomponent:@ "MV"]; NSString *temppath = [CachePath stringbyappendingpathcomponent:@ "Mvtemp"]; Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager]; if (![ FileManager Fileexistsatpath:filepath]) {[FileManager Createdirectoryatpath:filepath WithIntermediateDirectori Es:yes Attributes:nil Error:nil]; } if (![ FileManager Fileexistsatpath:temppath]) {[FileManager Createdirectoryatpath:temppath WithIntermediateDirectori Es:yes Attributes:nil Error:nil]; } nsstring *mp4temppath = [TempPath stringbyappendingpathcomponent:@ "Mv.temp"]; NSString *txttemPpath = [TempPath stringbyappendingpathcomponent:@ "Mv.txt"]; NSString *mp4path = [FilePath stringbyappendingpathcomponent:@ "Mv.mp4"]; Nsurl *url = [Nsurl urlwithstring:@ "Http://video.szzhangchu.com/1442395443772_5176326090.mp4"]; Nsurlrequest *request = [Nsurlrequest Requestwithurl:url]; unsigned long long downloadbytes = 0; if ([FileManager Fileexistsatpath:mp4temppath]) {downloadbytes = [self filesizeatpath:mp4temppath]; NSString *range = [NSString stringwithformat:@ "bytes=%llu-", downloadbytes]; Nsmutableurlrequest *mutablerequest = [request mutablecopy]; [Mutablerequest setvalue:range forhttpheaderfield:@ "range"]; request = Mutablerequest; } if (![ FileManager Fileexistsatpath:mp4path]) {self.operation = [[Afhttprequestoperation alloc] Initwithrequest:reque ST]; [Self.operation Setoutputstream:[nsoutputstream Outputstreamtofileatpath:mp4temppath Append:YES]]; __weak typeof (self) weakself = self; [_operation setdownloadprogressblock:^ (Nsuinteger bytesread, Long long totalbytesread, long long Totalbytesexpectedtoread) {weakSelf.progressView.progress = (float) (Totalbytesread + downloadbytes)/(Flo at) (Totalbytesexpectedtoread + downloadbytes); WeakSelf.progressLabel.text = [NSString stringwithformat:@ "%.2f%%", weakSelf.progressView.progress * 100]; NSString *progress = [NSString stringwithformat:@ "%.3f", weakSelf.progressView.progress]; [Progress Writetofile:txttemppath Atomically:yes encoding:nsutf8stringencoding Error:nil]; }]; [_operation setcompletionblockwithsuccess:^ (afhttprequestoperation *operation, id responseobject) {[FileManager moveitematpath:mp4temppath topath:mp4path Error:nil]; [FileManager Removeitematpath:txttemppath Error:nil]; } failure:^ (Afhttprequestoperation *operation, Nserror *error) {}]; [_operation start]; }} else {[sender settitle:@] starts downloading "forstate:uicontrolstatenormal"; [Self.operation Cancel]; _operation = nil; } }
-(unsigned long Long) Filesizeatpath: (NSString *) path{ unsigned long long fileSize = 0; Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager]; if ([FileManager Fileexistsatpath:path]) { Nserror *error = nil; Nsdictionary *dict = [FileManager attributesofitematpath:path error:&error]; if (dict &&!error) { fileSize = [Dict fileSize]; } } return fileSize;}
The final effect is as follows:
Third-party data requests used: afnetworking, everyone should have, here do not introduce
follow the daily new technology of Bo Master Weibo: Http://weibo.com/hanjunqiang
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS breakpoints Download Han Junqiang's Blog