IOS network analysis-(10 NSURLSession download Introduction), iosnsurlsession
I was planning to write two sections before, but I thought it was better to write one article when I started this article, which is helpful for understanding. NSURLSession download is implemented through the NSURLSession download proxy. The previous article also introduced the proxy. The reason why the download is not introduced is that I personally think it is easy to confuse (it should be too stupid ). This article will introduce the NSURLSession download implementation, including the start, suspension, and continuation of the download. I hope this film will bring you some gains. In the next article, I will introduce AFN. After all, AFN is the final principle in actual development.
//// ViewController. m // NSURLSession large file download /// Created by dahuan on 16/3/21. // Copyright©2016. all rights reserved. // # import "ViewController. h "@ interface ViewController () <NSURLSessionDownloadDelegate> @ property (nonatomic, strong) NSURLSessionDownloadTask * task; @ property (nonatomic, strong) NSData * resumeData; @ property (nonatomic, strong) NSURLSession * session; @ end @ implementation ViewController-(IBAction) start :( id) sender {NSURLSession * session = [NSURLSession sessionWithConfiguration: [NSURLSessionConfiguration defaultSessionConfiguration] delegate: self resume: [[NSOperationQueue alloc] init]; self. session = session; self. task = [session downloadTaskWithURL: [NSURL URLWithString: [@ "http: // 192.168.1.200/dom=.mp4" stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; [self. task resume];}-(IBAction) pause :( id) sender {// pause means to suspend the task [self. task cancelByProducingResumeData: ^ (NSData * _ Nullable resumeData) {// Save the downloaded data self. resumeData = resumeData;}];}-(IBAction) resume :( id) sender {// you can use ResumeData to create a Task self. task = [self. session downloadTaskWithResumeData: self. resumeData]; // enable the [self. task resume];}-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSLog (@ "% @", NSSearchPathForDirectoriesInDomains (9, 1, 1);}/* monitors the size of data downloaded from the temporary file. Each time a temporary file is written, it will call the total size of the bytes file written to bytesWritten at a time */-(void) URLSession :( NSURLSession *) session downloadTask (Bytes *) downloadTask didWriteData :( int64_t) bytesWritten totalBytesWritten :( int64_t) percent :( int64_t) percent {// print the download percentage NSLog (@ "% f", totalBytesWritten * 1.0/percent);} // download completed-(void) URLSession :( NSURLSession *) session downloadTask :( NSURLSessionDownloadTask *) Restart :( NSURL *) location {NSString * cachesPath = [[callback (NSCachesDirectory, NSUserDomainMask, YES) lastObject] resume: downloadTask. response. handler]; NSFileManager * mgr = [NSFileManager defaultManager]; [mgr moveItemAtURL: location toURL: [NSURL fileURLWithPath: cachesPath] error: NULL];}-(void) URLSession :( NSURLSession *) session task :( NSURLSessionTask *) task didCompleteWithError :( NSError *) error {NSLog (@ "% @", error);}-(void) didreceivemorywarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end