Introduction to NSURLSession using IOS7 APIs

Source: Internet
Author: User

Introduction to NSURLSession using IOS7 APIs

Ios7 introduces nsurlsession, which supports new features related to network operations in the background: 1. upload and download in the background; 2. you can pause and resume network operations without using nsoperation and nsurlsession APIs. 3. as a configurable container: for example, you can set some attributes of the http header and save them in the session, so you do not need to repeat the configuration 4. session5, which can be subclass and supports private configuration, improves the authentication callback. Previously, nsurlconnection's authentication callback could not match the request, and the callback may come from any request; now, each request can be processed in the specified proxy method.
Nsurlsession hierarchy:


As you can see, it includes configuration, proxy, and tasks used to process various tasks. Different http requests are implemented in the task. The task structure is as follows:


Vcbk1tDX08Dgu6/examples/u1xLLZ1/samples + + samples/samples + cve-vcd4kpha + Signature = "p1"> NSURLSessionConfiguration * config = [NSURLSessionConfiguration ephemeralSessionConfiguration];

// 2

[Config setHTTPAdditionalHeaders :@{@"Authorization": [Dropbox apiAuthorizationHeader]}];

// 3

_ Session = [NSURLSession sessionWithConfiguration: config];


Step 2:

Use the generated session to call the task method and save the task returned by the method. Execute the resume operation on the task (the generated task is suspended by default ).

There are two types of task Methods: taskwithurl and taskwithrequest. Simply put, the withurl method can be called only for url requests, if you need to add more content to the request for configuration, you need to use the taskwithrequest method.

The sample code is 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];


Step 3:

Parse the block implementation result in the previous request (nsdata needs to be parsed to understandable data, such as json, xml, and plist, based on the interface (usually determined by the server side ); you can also implement the relevant proxy method (you need to set the session proxy first in step 1) and process the relevant results/progress.

There are many urlsession-related proxy methods, which are not listed here. There are mainly the following sets of protocols:

@ Protocol NSURLSessionDelegate // Session proxy, equivalent to the parent class of all Protocols


@ Protocol NSURLSessionTaskDelegate // Task proxy. Its Parent protocol is NSURLSessionDelegate.


@ Protocol NSURLSessionDataDelegate // Datatask proxy. Its Parent protocol is NSURLSessionTaskDelegate.


@ Protocol NSURLSessionDownloadDelegate // Downloadtask proxy. Its Parent protocol is NSURLSessionDataDelegate.


Based on the hierarchical relationship of the corresponding classes, it is not difficult to understand the relationship between several groups of protocols.




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.