iOS網(wǎng)絡(luò)相關(guān)資料整理

1. NSURL,代表的是一個(gè)資源的地址,可以是網(wǎng)絡(luò)資源、本地資源、書(shū)簽等等。

An NSURL object represents a URL that can potentially contain the location of a resource on a remote server, the path of a local file on disk, or even an arbitrary piece of encoded data.

? ? 可以從中提取scheme、host、query等值。

2. NSURLRequest,代表是一個(gè)網(wǎng)絡(luò)請(qǐng)求,主要包含NSURL和從NSURL獲取內(nèi)容的緩存策略。

NSURLRequest objects represent a URL load request in a manner independent of protocol and URL scheme.

? ? 可以通過(guò)NSMutableURLRequest設(shè)置請(qǐng)求的Header、Method、Body等值。

3. NSURLConnection,代表一個(gè)網(wǎng)絡(luò)連接。

An NSURLConnection object lets you load the contents of a URL by providing a URL request object. The interface for NSURLConnection is sparse, providing only the controls to start and cancel asynchronous loads of a URL request. You perform most of your configuration on the URL request object itself.

? ? 可以用來(lái)發(fā)送同步或者異步請(qǐng)求,或者通過(guò)NSURLConnectionDataDelegate的方式來(lái)進(jìn)行更加精確的控制。比如可以用來(lái)顯示下載文件的進(jìn)度,這是Demo

4. NSURLSession,從iOS7之后提供的代替NSURLConnection的方案。

The NSURLSession class and related classes provide an API for downloading content. This API provides a rich set of delegate methods for supporting authentication and gives your app the ability to perform background downloads when your app is not running or, in iOS, while your app is suspended.

? ? 提供了安全驗(yàn)證和證書(shū)機(jī)制,能夠在App進(jìn)入后臺(tái)之后進(jìn)行上傳或者下載任務(wù),用一個(gè)session來(lái)管理一些任務(wù),提供了更多的deletege方法和相關(guān)任務(wù)類進(jìn)行更加精確的控制,通過(guò)NSURLSessionConfiguration對(duì)session進(jìn)行配置,session中的所有task共享同一個(gè)配置,值得一提的是NSURLSession保持著對(duì)delegate和task的強(qiáng)引用,調(diào)用完成之后需要調(diào)用invalidate相關(guān)方法,否則會(huì)造成內(nèi)存泄漏。這里提供了一個(gè)演示的Demo

5. NSURLSessionConfiguration,對(duì)NSURLSession提供配置,包括緩存策略、超時(shí)等。

An NSURLSessionConfiguration object defines the behavior and policies to use when uploading and downloading data using an?NSURLSession?object. When uploading or downloading data, creating a configuration object is always the first step you must take. You use this object to configure the timeout values, caching policies, connection requirements, and other types of information that you intend to use with your NSURLSession object.

6. NSURLSessionTask,一個(gè)任務(wù)對(duì)應(yīng)一個(gè)request,task可以restart,而NSURLConnection只能啟動(dòng)一次。這是其他Task的基類:NSURLSessionDataTask提供一般形式的NSData的請(qǐng)求;NSURLSessionUploadTask:提供上傳feature并能在App suspended的情況下執(zhí)行;NSURLSessionDownloadTask:提供下載feature并能在App suspended的情況下執(zhí)行。


相關(guān)資料:

1.?NSURLConnection Class Reference

2.?NSURLSession Class Reference

3.?NSURLProtocol

4.?From NSURLConnection to NSURLSession

NSURLProtocol by nshipster

5.?HTTP_pipelining

6.?SPDY

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容