IOS large file breakpoint download, iOS file breakpoint download

Source: Internet
Author: User

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

 

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.