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.