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