天天看點

ios7新特性nsurlsession初探

1.背景上傳下載下傳;

2.不需要通過nsoperation,我們用nsurlsession的api就可以做到網絡操作的暫停和恢複。

3.作為可配置的容器:例如你可以設定http頭的一些屬性并且儲存在session裡,你就不用重複去做配置了;don't repeat yourself~

4.可以子類化并支援私有的存儲;

5.對鑒權的回調做了改進,此前nsurlconnection的鑒權回調無法和請求進行比對,該回調可能來自任意的請求;而現在每個請求都可以在指定的代理方法中對其進行處理.

翻譯材料如下:(來自連結:http://www.raywenderlich.com/51127/nsurlsession-tutorial)

Why should you use <code>NSURLSession</code>? Well, it brings

you a number of new advantages and benefits:

Background uploads and downloads: With just a configuration option when the <code>NSURLSession</code> is

created, you get all the benefits of background networking. This helps with battery life, supports UIKit multitasking and uses the same delegate model as in-process transfers.

Ability to pause and resume networking operations: As you will see later, with the

NSURLSession API any networking task can be paused, stopped, and restarted. No NSOperation sub-classing necessary.

Configurable container: Each <code>NSURLSession</code> is

the configurable container for putting requests into. For example, if you need to set an HTTP header option you will only need to do this once and each request in the session will have the same configuration.

Subclassable and private storage: <code>NSURLSession</code> is

subclassable and you can configure a session to use private storage on a per session basis. This allows you to have private storage objects outside of the global state.

Improved authentication handling: Authentication is done on a specific connection basis.

When using <code>NSURLConnection</code> if an authentication

challenge was issued, the challenge would come back for an arbitrary request, you wouldn’t know exactly what request was getting the challenge. With<code>NSURLSession</code>,

the delegate handles authentication.