Use nsurlsession for file breakpoint download in iOS

Source: Internet
Author: User

The first part of the Knowledge reserve

1. Understanding of the Nsurlsesiion

Nsurlsesiion is a class introduced by Apple in IOS7, which has the nsurlconnection approach and is more powerful than it. The aim of Apple's launch is to replace Nsurlconnection's trend or purpose.

2. The role of Nsurlsesiion

Implement the download and upload of the file. In nsurlsesiion, any request can be viewed as a task. And Nsurlsesiiondata has two sub-classes:nsurlsessiondownlaodtask Implementation of file download and nsurlsessionuploadtask implementation of file upload.

3.NSURLSession of Access

The acquisition of Nsurlsession can be obtained by means of nsurlsessiondownloadtaskdelegate, but this protocol must be followed. Get the following:

nsurlsessiondonfiguration* configuration=[nsurlsessiondonfiguration defaultsessionconfiguration];

Self.session=[nsurlsession sessionwithconfiguration:nsurlsessiondonfiguration delegate:self delegateQueue:[ Nsoperationqueue Mainqueue]];

4. Download Task creation

nsurlsessiondownlaodtask* Task=[self.session Downloadtaskwithurl:url]

Proxy method for 5.NSURLSessionTaskdelegate:

It has three methods:

6. Getting the sandbox path

NSString *path=[nssearchpathfordiretoriesindomains (Nsdocumentdirectoriy, Nsuserdomainmask, YES) lastObject];

7.caches path acquisition and creation of the filename inside

nsstring *caches=[nssearchpathfordiretoriesindomains (Nscachesdirectoriy, Nsuserdomainmask, YES) lastObject];

nsstring* filename=[caches StringByAppendingPathComponent:downloadTask.response.suggestedname];

description of several properties of the program in Part two

1. Resumedata

This parameter contains the location information to continue downloading the file. That is, when you download 10M of file data, it is paused. The next time you continue downloading, you will start at the 10th m position instead of downloading from the beginning of the file. Thus, in order to preserve this information, this attribute of the NSData type is defined:resumedata

2. Task

The type of the parameter is nsurlsessiondownloadtask. Because you have to have this property when the program calls pause (pause), how do I get it? The best way to do this is to have the controller have this property.

3. Session

The type of the parameter is nsurlsession. This session must be owned by the program call to continue the download (resume). Because the task was canceled and cannot be reused, the lazy Load method allows the session to be created only once, and the controller has this property.

The third part of the program implementation process (pseudo-code)

Here is the idea of file download:


part Fourth UI Setup

Because the program UI interface is easy to build, it is not described in detail here.


part fifth procedure special attention points

1. There is a certain risk in calling pause, because self has a strong reference to the task, the task also references the block, and the block also references self, which forms a loop to use. How to solve this problem?

Workaround: Weak reference __weak typedef (self) for self vc=self

2. If the completion and block are set and the proxy method is implemented, the program takes precedence of the block

The sixth part of the Code



part Seventh procedure





Use nsurlsession for file breakpoint download in iOS

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.