iOS网络相关资料整理

1. NSURL,代表的是一个资源的地址,可以是网络资源、本地资源、书签等等。

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,代表是一个网络请求,主要包含NSURL和从NSURL获取内容的缓存策略。

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

    可以通过NSMutableURLRequest设置请求的Header、Method、Body等值。

3. NSURLConnection,代表一个网络连接。

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.

    可以用来发送同步或者异步请求,或者通过NSURLConnectionDataDelegate的方式来进行更加精确的控制。比如可以用来显示下载文件的进度,这是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.

    提供了安全验证和证书机制,能够在App进入后台之后进行上传或者下载任务,用一个session来管理一些任务,提供了更多的deletege方法和相关任务类进行更加精确的控制,通过NSURLSessionConfiguration对session进行配置,session中的所有task共享同一个配置,值得一提的是NSURLSession保持着对delegate和task的强引用,调用完成之后需要调用invalidate相关方法,否则会造成内存泄漏。这里提供了一个演示的Demo。

5. NSURLSessionConfiguration,对NSURLSession提供配置,包括缓存策略、超时等。

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,一个任务对应一个request,task可以restart,而NSURLConnection只能启动一次。这是其他Task的基类:NSURLSessionDataTask提供一般形式的NSData的请求;NSURLSessionUploadTask:提供上传feature并能在App suspended的情况下执行;NSURLSessionDownloadTask:提供下载feature并能在App suspended的情况下执行。


相关资料:

1. NSURLConnection Class Reference

2. NSURLSession Class Reference

3. NSURLProtocol

4. From NSURLConnection to NSURLSession

NSURLProtocol by nshipster

5. HTTP_pipelining

6. SPDY

你可能感兴趣的:(iOS网络相关资料整理)