Network Programming for iOS development-2. Download The NSURLSessionDownloadTask file and nsurlsessiondatatask

Source: Internet
Author: User

Network Programming for iOS development-2. Download The NSURLSessionDownloadTask file and nsurlsessiondatatask

 

Content outline:

1. Review NSURLSessionTask

2. NSURLSessionDownloadTask large file block download

3. Download The NSURLSessionDownloadTask large file using a proxy method

4. The NSURLSessionDownloadTask large file proxy method for resumable download

 

If the reader is reading or learning about the NSURLSession to be introduced in this article for the first time, it is better to read my previous article "network programming for iOS development-1. Basic use of NSURLSession" and then learn this article.

 

1. Review NSURLSessionTask:

NSURLSessionTask is required to use NSURLSession. Therefore, we need to know the relationship between the class of the Task and its subclass:

    

This article describes how to download large files. Since it is "Download", NSURLSessionDownloadTask is used naturally.

 

2. NSURLSessionDownloadTask large file block download

API: http: // 120.25.226.186: 32812/resources/images/minion_08.png)

2-1. Class, method, and attribute introduction:

NSURLSession

+ (NSURLSession *) sharedSession; for a single instance, create a session object directly.

-(NSURLsessionDownloadTask *) downloadTaskWithURL: completionHandler:; creates a download task downloadTask for the session based on the url. When the download task is completed, the callback block is called.

NSURLsessionDownloadTas

-(Void) resume; Start and execute the task

 

2-2. Source Code display:

2-3. Disadvantages:

The Block download method is not suitable for downloading large files, because this method will call back the block parameter after completionHandler after the File Download is completed.

Obtain location, response, and error ).

In this way, for large files, we cannot obtain the download progress in real time during the download process.

 

3. Download The NSURLSessionDownloadTask large file using a proxy method

Tested API: http: // 120.25.226.186: 32812/resources/videos/minion_02.mp4, it will not take you tens of minutes to download.

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.