iOS breakpoints Download Han Junqiang's Blog

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.