IOS network analysis-(10 NSURLSession download Introduction), iosnsurlsession

Source: Internet
Author: User

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

 

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.