Network Programming for iOS development-3. NSURLSessionDataTask implements file download (offline resumable download) and nsurlsession resumable download

Source: Internet
Author: User

Network Programming for iOS development-3. NSURLSessionDataTask implements file download (offline resumable download) and nsurlsession resumable download

NSURLSessionDownloadTask is used to download large files without the need for offline resumable data transfer.

For more information about NSURLSessionDownloadTask, read the previous article: Network Programming for iOS development-2. Download NSURLSessionDownloadTask.

In addition, NSURLSessionDownloadTask and NSURLSessionDataTask are both upper-layer inheritance extensions of NSURLSessionTask. For example, NSURLSessionDownloadTask implements

The file is downloaded and stored in the sandbox. You need to use NSURLSessionDataTask to complete this process by yourself. However, NSURLSessionDataTask can implement offline resumable data transfer.

 

The basic idea of implementing offline resumable data transfer is as follows:

1. For example, if the app downloads a small video of 15 MB, but the app may be downloaded to 5 MB because the user needs to quit or unexpectedly crash or quit or the network is disconnected, resulting in 10 MB of failure to download the video.

2. In order to continue downloading the remaining 10 M after restarting the program, when the program continues downloading, You need to determine and read whether the downloaded data and data length exist in the local cache, and then continue downloading the remaining part after the data length.

 

API used to test the code: http: // 120.25.226.186: 32812/resources/videos/minion_02.mp4

 

1. NSURLSessionDataTask implements file download (offline resumable download is not implemented)

1-1. programming logic Implementation Details: <a> Create a session and request, and then create a request Task and start the task. <B> Add the Protocol <NSURLSessionDataDelegate>, set the proxy, and implement the proxy method. <C> obtain the file name and the length of the file to be downloaded from the response proxy method returned by the server, splice the sandbox path, create an output stream based on the path, and enable the output stream. Do not forget to set the server to receive the response. <D> in the Data proxy method returned by the server, the output stream is constantly used to write data and the progress bar is set through the obtained data. <E> in the proxy method that ends the server request, close the output stream and release the pointer of the output stream. In addition, you may need to print the error message. 1-2. Code implementation: 2. NSURLSessionDataTask implements file download (implements offline resumable download)2-1. Programming Implementation Details: <a> concatenate the sandbox path based on the file name to directly obtain the attributes of the file. If the file does not exist, nil is returned, if the file exists, one of the attributes of the file is read: the length of the file data. <B> Create a session and a variable request, set the request header, and notify the server that the returned data starts to be downloaded after the length of the downloaded file. Finally, the session creates a request Task and starts the task. <C> Add the Protocol <NSURLSessionDataDelegate>, set the proxy, and implement the proxy method. <D> obtain the file name and the length of the file to be downloaded from the response proxy method returned by the server, splice the sandbox path, create an output stream based on the path, and enable the output stream. Do not forget to set the server to receive the response. <E> in the Data proxy method returned by the server, the output stream is constantly used to write data and the progress bar is set through the obtained data. <F> in the proxy method that ends the server request, close the output stream and release the pointer of the output stream. In addition, you may need to print the error message. <G> do not forget to release the session object (either of the two methods) 2-2. Source Code display:

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.