Breakpoint download and breakpoint

Source: Internet
Author: User

Breakpoint download and breakpoint

The demo is as follows:

 

Use NSURLSession for breakpoint download

 

1. Attributes

@ Interface ViewController () <NSURLSessionDownloadDelegate>/** download task */@ property (nonatomic, strong) NSURLSessionDownloadTask * task;/** last download information */@ property (nonatomic, strong) NSData * resumeData;/** session */@ property (nonatomic, strong) NSURLSession * session; @ end

 

2. initialize attributes

 

-(NSURLSession *) session {if (! _ Session) {_ session = [NSURLSession sessionWithConfiguration: [NSURLSessionConfiguration defaultSessionConfiguration] delegate: self delegateQueue: [[NSOperationQueue alloc] init];} return _ session;} (NSData *) resumeData {// The first access to resumeData loads the resumeData in the sandbox if (! _ ResumeData) {NSString * caches = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSString * file = [caches stringByAppendingPathComponent: @ "data"]; _ resumeData = [NSData dataWithContentsOfFile: file];} return _ resumeData ;}

 

3. Click events

 

/*** Start download */-(IBAction) start :( id) sender {// obtain the download task self. task = [self. session downloadTaskWithURL: [NSURL URLWithString: @ "http: // 120.25.226.186: 32812/resources/videos/minion_02.mp4"]; // start the task [self. task resume];}/*** pause download */-(IBAction) pause :( id) sender {// once this task is canceled, the last download information [self. task cancelByProducingResumeData: ^ (NSData * _ Nullable resumeData) {self. resumeData = resumeData; // you can put resumeData in the sandbox NSString * caches = [Export (NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSString * file = [caches stringByAppendingPathComponent: @ "data"]; [resumeData writeToFile: file atomically: YES];}/*** continue to download */-(IBAction) goOn :( id) sender {self. task = [self. session downloadTaskWithResumeData: self. resumeData]; [self. task resume];}

 

4. Implement proxy Methods

 

# Pragma mark-<strong>-(void) URLSession :( NSURLSession *) session downloadTask :( NSURLSessionDownloadTask *) downloadTask didResumeAtOffset :( int64_t) fileOffset expectedTotalBytes :( int64_t) expectedTotalBytes {NSLog (@ "% s" ,__ func _);}/*** each time data is written to a temporary file, a * totalBytesExpectedToWrite: total size * totalBytesWritten: Written size * bytesWritten write size */-(void) URLSession :( NSURLSession *) session downloadTask :( NSURLSessionDownloadTask *) downloadTask didWriteData :( int64_t) bytesWritten totalBytesWritten :( int64_t) totalBytesWritten totalBytesExpectedToWrite :( int64_t) bytes {NSLog (@ "% s" ,__ func _); NSLog (@ "------ % f ", 1.0 * totalBytesWritten/totalBytesExpectedToWrite);}/*** call this method once the download is complete */-(void) URLSession :( NSURLSession *) session downloadTask :( NSURLSessionDownloadTask *) downloadTask didFinishDownloadingToURL :( NSURL *) location {NSLog (@ "% s" ,__ func _); // The actual path of the file to be stored in the future NSString * file = [[NSSearchPathForDirectoriesInDomains, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: downloadTask. response. suggestedFilename]; // cut the temporary file of location to the actual path NSFileManager * manager = [NSFileManager defaultManager]; [manager moveItemAtURL: location toURL: [NSURL fileURLWithPath: file] error: nil];}-(void) URLSession :( NSURLSession *) session task :( NSURLSessionTask *) task didCompleteWithError :( NSError *) error {NSLog (@ "% s ", __func __);

// Save and restore Data

Self. resumeData = error. userInfo [NSURLSessionDownloadTaskResumeData];

}

 

How to Implement resumable download?

The last downloaded information is saved in resumeData.

/**

* Request the http: // 120.25.226.186: 32812/resources/videos/minion_02.mp4 path

* Set request headers

* Range: 0-100

* [[NSMutableURLRequest requestWithURL: nil] setValue: @ "0-1024" forHTTPHeaderField: @ "Range"];

* By setting the Range request header, you can control which part of the information to download.

*/

These are all done for you in resumeData.

 

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.