IOS Large File Breakpoint download

Source: Internet
Author: User

When downloading large files, IOS may cause the download to be interrupted because of network or human, so how can I download the breakpoint?

//file path for Resumedata#defineXmgresumedatafile [[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) LastObject] stringbyappendingpathcomponent:@ "Resumedata.tmp"]#import "ViewController.h"@interfaceViewcontroller () <NSURLSessionDownloadDelegate>/** Download Task*/@property (nonatomic, strong) Nsurlsessiondownloadtask*task;/** Save last Download Information*/@property (nonatomic, strong) NSData*Resumedata;/** Session*/@property (nonatomic, strong) Nsurlsession*session;@end

@implementationViewcontroller/** Session*/-(Nsurlsession *) session{if(!_session) {_session= [Nsurlsession sessionwithconfiguration:[nsurlsessionconfiguration defaultsessionconfiguration]Delegate: Self Delegatequeue:[[nsoperationqueue alloc] [init]]; }    return_session;}/** * Start download*/-(Ibaction) Start: (ID) Sender {Self.task= [Self.session downloadtaskwithurl:[nsurl urlwithstring:@"Http://120.25.226.186:32812/resources/videos/minion_01.mp4"]]; [Self.task resume];}/** * Pause Download*/-(Ibaction) Pause: (ID) Sender {//Once this task is canceled, it cannot be recovered.[Self.task cancelbyproducingresumedata:^ (NSData *resumedata) {Self.resumedata=Resumedata; }];}/** * Continue to download*/-(Ibaction) GoOn: (ID) Sender {Self.task=[Self.session DownloadTaskWithResumeData:self.resumeData]; [Self.task resume];}#pragmaMark-<nsurlsessiondownloaddelegate>-(void) Urlsession: (Nsurlsession *) session Task: (Nsurlsessiontask *) Task Didcompletewitherror: (Nserror *) error{NSLog (@"Didcompletewitherror"); //Save Recovery DataSelf.resumedata =error.userinfo[nsurlsessiondownloadtaskresumedata];}- (void) Urlsession: (Nsurlsession *) session Downloadtask: (Nsurlsessiondownloadtask *) Downloadtask Didresumeatoffset: (int64_t) Fileoffset expectedtotalbytes: (int64_t) expectedtotalbytes{NSLog ( @"Didresumeatoffset");}/** * This method is called whenever the data is written to a temporary file * totalbytesexpectedtowrite: Total size * Totalbyteswritten: Size already written * Byteswritten: How much this is written*/- (void) Urlsession: (Nsurlsession *) session Downloadtask: (Nsurlsessiondownloadtask *) Downloadtask Didwritedata: (int64_t) Byteswritten Totalbyteswritten: (int64_t) Totalbyteswritten Totalbytesexpectedtowrite: (int64_t) totalbytesexpectedtowrite{NSLog (@"--------%f",1.0* Totalbyteswritten/totalbytesexpectedtowrite);}/** * * This method is called once the download is complete*/- (void) Urlsession: (Nsurlsession *) session Downloadtask: (Nsurlsessiondownloadtask *) downloadtask Didfinishdownloadingtourl: (Nsurl *) location{//the true path of the file for future storageNSString *file =[[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) Lastobject]        StringByAppendingPathComponent:downloadTask.response.suggestedFilename]; //cut temporary files from location to the real pathNsfilemanager *mgr =[Nsfilemanager Defaultmanager]; [Mgr Moveitematurl:location Tourl:[nsurl fileurlwithpath:file] error:nil];}@end

IOS Large File Breakpoint download

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.