Introduction to IOS7 introduction of API Nsurlsession

Source: Internet
Author: User

iOS7 introduces Nsurlsession, which can support the new features of background-related network operations:1. Background upload and download;2. No need to pass the nsoperation, directly with the Nsurlsession API can be done to suspend and resume network operations. 3. As a configurable container: for example, you can set some properties of the HTTP header and save it in the session without repeating the configuration4. Session that can subclass and support private configuration5. An improvement has been made to the authentication callback, which could have come from any request, and now each request can be processed in the specified proxy method, after the Nsurlconnection authentication callback could not match the request .
nsurlsession Hierarchical structure:


as you can see, it contains a task that configures, proxies, and handles various tasks, and implements various HTTP requests within the task. The structure diagram of the task is as follows:



We can see that the sub-class uploads the downloaded task, and supports the Suspend/resume/cancel operation; The return nsdata needs to be handled by itself, which is less than the afnetworking of convenience, after all afnetworking is encapsulated at a higher level.

To perform a standard network operation we are divided into the following steps:

Step1: Configuring the session, such as configuring the header of the request, configuring whether to save the cache, cookies, certificates, etc.

The code examples are as follows:


nsurlsessionconfiguration *config = [nsurlsessionconfiguration Ephemeralsessionconfiguration];

//2

[config sethttpadditionalheaders:@{@"Authorization": [Dropbox apiauthorizationheader]};

//3

_session =[nsurlsession sessionwithconfiguration:config];


Step2:

Use the generated session to invoke the task method, save the task returned by the method ; Perform a resume operation on the task (the default generated task is in a pending state).

There are two types of task methods here, one is taskwithurl, and the other is Taskwithrequest, which is simple to invoke only for URL requests Withurl method, taskwithrequest -related methods are required if you need to have more content in the request for some configuration.

The code examples are as follows:

Nsurl *URL= [Dropbox approoturl];

2

Nsurlsessiondatatask *datatask =

[self.session datataskwithurl:URL

Completionhandler: ^ (nsdata*data,

Nsurlresponse *Response,

Nserror *error){

if (! Error){

//TODO 1:more coming here!

}

}];

3

[datatask resume];


Step3:

The block implementation results in the previous step request parsing (NSData needs to be parsed into understandable data, such as json/xml/plist , depending on the specifics of the interface (usually the server side); It is also possible not to perform related operations in blocks, but to implement the relevant proxy method (which requires the agent of the session to be set up in Step1 ), to process related results/progress, etc.

urlsession Related agent methods There are many, not listed here, mainly have the following groups of protocols:

@protocol nsurlsessiondelegate <NSObject>//Session Proxy, equivalent to the parent class of all protocols


@protocol nsurlsessiontaskdelegate <NSURLSessionDelegate>//Task 's proxy, whose parent agreement is Nsurlsessiondelegate


@protocol nsurlsessiondatadelegate <NSURLSessionTaskDelegate>//Datatask Proxy, whose parent agreement is Nsurlsessiontaskdelegate


@protocol nsurlsessiondownloaddelegate <NSURLSessionTaskDelegate> //Downloadtask Proxy, whose parent agreement is nsurlsessiondatadelegate


In combination with the hierarchical relationship of its corresponding classes, the relationship of the related groups of protocols is not difficult to understand.




IOS7 Introduction API Nsurlsession Introduction

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.