IOS large file breakpoint download, iOS file breakpoint download
When iOS downloads large files, the download may be interrupted due to network or human reasons. How can I perform resumable download?
// ResumeData file path # define XMGResumeDataFile [[NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: @ "resumeData. tmp "] # import" ViewController. h "@ interface ViewController () <NSURLSessionDownloadDelegate>/** download task */@ property (nonatomic, strong) NSURLSessionDownloadTask * task; /** save the last download information */@ property (nonatomic, strong) NSData * resumeData;/* session */@ property (nonatomic, strong) NSURLSession * session; @ end
@ Implementation ViewController/** 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, [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] ;}# pragma mark-<NSURLSessionDownloadDelegate>-(void) URLSession :( NSURLSession *) session task :( NSURLSessionTask *) task didCompleteWithError :( NSError *) error {NSLog (@ "didCompleteWithError"); // save and restore Data self. resumeData = error. userInfo [callback];}-(void) URLSession :( NSURLSession *) session downloadTask :( NSURLSessionDownloadTask *) downloadTask didResumeAtOffset :( int64_t) fileOffset expectedTotalBytes :( int64_t) expectedTotalBytes {NSLog (@ "didResumeAtOffset");}/*** this method is called once every time data is written to a temporary file. * totalBytesExpectedToWrite: total size * totalBytesWritten: size of data written * bytesWritten: the number of data written this time */-(void) URLSession :( NSURLSession *) session downloadTask :( bytes *) downloadTask didWriteData :( int64_t) bytesWritten bytes :( int64_t) totalBytesWritten totalBytesExpectedToWrite :( int64_t) totalBytesExpectedToWrite {NSLog (@ "-------- % f", 1.0 * totalBytesWritten/totalBytesExpectedToWrite );} /*** this method will be called once the download is complete */-(void) URLSession :( NSURLSession *) session downloadTask :( NSURLSessionDownloadTask *) downloadTask didFinishDownloadingToURL :( NSURL *) location {// The actual path of the file to be stored in the future NSString * file = [[NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: downloadTask. response. suggestedFilename]; // cut the temporary file of location to the actual path NSFileManager * mgr = [NSFileManager defaultManager]; [mgr moveItemAtURL: location toURL: [NSURL fileURLWithPath: file] error: nil];} @ end