About the URL Loading System
This guide describes the Foundation framework classes available for interacting with URLs and communicating with servers using standard Internet protocols. Together these classes are referred to as the URL loading system.
本指南描述了Foundation框架下可用于跟URL互動并且通過標準的網絡協(xié)議與服務器交流的一些類。綜合這些類就構成了URL加載系統(tǒng)。
The URL loading system is a set of classes and protocols that allow your app to access content referenced by a URL. At the heart of this technology is the NSURL class, which lets your app manipulate URLs and the resources they refer to.
URL加載系統(tǒng)是一系列的允許你的應用訪問URL內容類和協(xié)議蜜宪。這項技術的核心就是NSURL類,NSURL類使你的app能夠操作URL和URL所涉及的資源劫流。
To support that class, the Foundation framework provides a rich collection of classes that let you load the contents of a URL, upload data to servers, manage cookie storage, control response caching, handle credential storage and authentication in app-specific ways, and write custom protocol extensions.
為了支持URL類踪危,Foundation框架提供了一個非常強大的類合集憋活,以使你能夠讀取URL的內容堕虹,上傳數(shù)據(jù)到服務器卧晓,cookie存儲,管理響應緩存赴捞,以自己的方式處理證書存儲和驗證逼裆,還有自定義一些協(xié)議擴展。
The URL loading system provides support for accessing resources using the following protocols:
URL加載系統(tǒng)支持以下獲取資源的協(xié)議:
- File Transfer Protocol (ftp://)
- Hypertext Transfer Protocol (http://)
- Hypertext Transfer Protocol with encryption (https://)
- Local file URLs (file:///)
- Data URLs (data://)
It also transparently supports both proxy servers and SOCKS gateways using the user’s system preferences.
顯然赦政,它也支持代理服務器和用戶系統(tǒng)設置的SOCKS網關(胜宇?)
At a Glance 概述
The URL loading system includes classes that load URLs along with a number of important helper classes that work with those URL loading classes to modify their behavior. The major helper classes fall into five categories: protocol support, authentication and credentials, cookie storage, configuration management, and cache management.
URL加載系統(tǒng)包含很多加載URL的類和其他的一些很重要的修飾輔助類,加載類和輔助類共同作用。主要的輔助類有五種:協(xié)議支持桐愉,證書認證财破,cookie存儲,配置管理和緩存管理从诲。
URL Loading
The most commonly used classes in the URL loading system allow your app to retrieve the content of a URL from the source. You can retrieve that content using NSURLSession. The specific methods you use depend largely on whether you wish to fetch data to memory or download it to disk.
URL加載系統(tǒng)中最常用的類左痢,app通過這些類可以從數(shù)據(jù)源上檢索URL的內容≌登螅可用NSURLSession類檢索URL內容抖锥。具體用法很大程度上取決于你僅僅想讀取數(shù)據(jù)到內存還是寫到磁盤上亿眠。
Fetching Content as Data (In Memory) 讀取到內存中
At a high level, there are two basic approaches to fetching URL data:
For simple requests, use the NSURLSession API to retrieve the contents from an NSURL object directly, either as an NSData object or as a file on disk.
For more complex requests—requests that upload data, for example—provide an NSURLRequest object (or its mutable subclass, NSMutableURLRequest) to NSURLSession.
通常有兩種URL數(shù)據(jù)讀取辦法:
- 簡單請求却音,使用NSURLSession接口直接檢索一個NSURL對象酷愧,得到一個NSData對象或者磁盤上的文件作為返回。
- 高級的上傳數(shù)據(jù)的用法,比如給NSURLSession傳入一個NSURLRequset對象(或者可變子類昂儒,NSMutableURLRequest*)。
Regardless of which approach you choose, your app can obtain the response data in two ways:
- Provide a completion handler block. The URL loading class calls that block when it finishes receiving data from the server.
- Provide a custom delegate. The URL loading class periodically calls your delegate methods as it receives the data from the originating source. Your app is responsible for accumulating that data, if needed.
In addition to the data itself, the URL loading class provides your delegate or completion handler block with a response object that encapsulates metadata associated with the request, such as the MIME type and content length.
不管你選擇那種辦法冯事,app能通過以下兩種方式獲得返回數(shù)據(jù):
- 完成回調的block.URL加載類會在從服務器上獲取數(shù)據(jù)后調用這個block.
- 自定義代理泼疑,URLJ加載類一收到服務器的返回數(shù)據(jù)就會周期性的調用代理方法,如果有需要的話岔帽,app就會多次收到返回數(shù)據(jù)玫鸟。
除了data本身,URL加載類還會提供給的你代理或者完成回調block一個封裝好這次請求相關數(shù)據(jù)(比如數(shù)據(jù)長度和MIME)的響應對象犀勒。
相關章節(jié):Using NSURLSession
Downloading Content as a File 下載
At a high level, there are two basic approaches to downloading the contents of a URL to a file:
- For simple requests, use the NSURLSession API to retrieve the contents from an NSURL object directly, either as an NSData object or as a file on disk.
- For more complex requests—requests that upload data, for example—provide an NSURLRequest object (or its mutable subclass, NSMutableURLRequest) to NSURLSession.
Note: Downloads initiated by an NSURLSession instance are not cached. If you need to cache the results, your app must use either NSURLSession and write the data to disk itself.
通常有兩種下載的辦法:
- 簡單請求屎飘,使用NSURLSession接口直接檢索一個NSURL對象,得到一個NSData對象或者磁盤上的文件作為返回贾费。
- 高級的上傳數(shù)據(jù)的用法钦购,比如給NSURLSession傳入一個NSURLRequset對象(或者可變子類,NSMutableURLRequest*)褂萧。
跟上面一樣
Note:用NSURLSession下載是沒有緩存的押桃。如果需要緩存下載結果,你必須自己寫文件到磁盤和NSURLSession配合使用导犹。
Helper Classes
The URL loading classes use two helper classes that provide additional metadata—one for the request itself (NSURLRequest) and one for the server’s response (NSURLResponse).
URL加載類有兩個提供額外數(shù)據(jù)的輔助類:本地請求(NSURLRequest)和服務返回(NSURLResponse)
URL Requests
An NSURLRequest object encapsulates a URL and any protocol-specific properties, in a protocol-independent manner.
Note: When a client app initiates a connection or download using an instance of NSMutableURLRequest, a deep copy is made of the request. Changes made to the initiating request have no effect after a download is initialized.
Some protocols support protocol-specific properties. For example, the HTTP protocol adds methods to NSURLRequest that return the HTTP request body, headers, and transfer method. It also adds methods to NSMutableURLRequest to set those values.
The details of working with URL request objects are described throughout this book.
一個NSURLRequest對象封裝了1個URL以及相應的協(xié)議信息唱凯。
Note:當客戶端app使用NSMutableURLRequest向服務器發(fā)起連接或者下載時,request會進行深拷貝,在下載初始化完成后修改request不會影響之前的設置.
一些協(xié)議支持設置其特有的屬性.比如HTTP協(xié)議,在NSURLRequest中添加了返回HTTP請求體、請求頭以及轉換方法.同樣NSMutableURLRequest也加入了這些方法谎痢。
具體怎樣使用URL請求對象的辦法貫穿在整篇指南中磕昼。
Redirection and Other Request Changes
Some protocols, such as HTTP, provide a way for a server to tell your app that content has moved to a different URL. The URL loading classes can notify their delegates when this happens. If your app provides an appropriate delegate method, your app can then decide whether to follow the redirect, return the response body from the redirect, or return an error.
一些協(xié)議,比如HTTP,當目標URL遷移時會通過某種方式通知客戶端.當這種情況發(fā)生時,URL加載類會通知其代理.如果你的app實現(xiàn)了對應的代理方法,你可以選擇重新請求定向后的地址還是返回一個錯誤信息.
Authentication and Credentials
Some servers restrict access to certain content, requiring a user to authenticate by providing some sort of credentials—a client certificate, a user name and password, and so on—in order to gain access. In the case of a web server, restricted content is grouped into a realm that requires a single set of credentials. Credentials (certificates, specifically) are also used to determine trust in the other direction—to evaluate whether your app should trust the server.
The URL loading system provides classes that model credentials and protected areas as well as providing secure credential persistence. Your app can specify that these credentials persist for a single request, for the duration of an app’s launch, or permanently in the user’s keychain.
一些服務器限制某些權限,要求通過一些證書舶得、用戶名密碼等等來獲取權限掰烟。在web服務器方面,限制內容被分組到一個集合,需要單獨的一套認證。證書也可以被用來確認是否信任所訪問的服務器.
Note: Credentials stored in persistent storage are kept in the user’s keychain and shared among all apps.
Note: 證書會一直在用戶鑰匙串中存儲纫骑,并且所有的app共用蝎亚。