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.