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互动并且通过标准的网络协议与服务器交流的一些类。综合这些类就构成了URL加载系统。
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加载系统是一系列的允许你的应用访问URL内容类和协议。这项技术的核心就是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的内容,上传数据到服务器,cookie存储,管理响应缓存,以自己的方式处理证书存储和验证,还有自定义一些协议扩展。
The URL loading system provides support for accessing resources using the following protocols:
URL加载系统支持以下获取资源的协议:
- 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.
显然,它也支持代理服务器和用户系统设置的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加载系统包含很多加载URL的类和其他的一些很重要的修饰辅助类,加载类和辅助类共同作用。主要的辅助类有五种:协议支持,证书认证,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加载系统中最常用的类,app通过这些类可以从数据源上检索URL的内容。可用NSURLSession类检索URL内容。具体用法很大程度上取决于你仅仅想读取数据到内存还是写到磁盘上。
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数据读取办法:
- 简单请求,使用NSURLSession接口直接检索一个NSURL对象,得到一个NSData对象或者磁盘上的文件作为返回。
- 高级的上传数据的用法,比如给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能通过以下两种方式获得返回数据:
- 完成回调的block.URL加载类会在从服务器上获取数据后调用这个block.
- 自定义代理,URLJ加载类一收到服务器的返回数据就会周期性的调用代理方法,如果有需要的话,app就会多次收到返回数据。
除了data本身,URL加载类还会提供给的你代理或者完成回调block一个封装好这次请求相关数据(比如数据长度和MIME)的响应对象。
相关章节: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对象或者磁盘上的文件作为返回。
- 高级的上传数据的用法,比如给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加载类有两个提供额外数据的辅助类:本地请求(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以及相应的协议信息。
Note:当客户端app使用NSMutableURLRequest向服务器发起连接或者下载时,request会进行深拷贝,在下载初始化完成后修改request不会影响之前的设置.
一些协议支持设置其特有的属性.比如HTTP协议,在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.
一些协议,比如HTTP,当目标URL迁移时会通过某种方式通知客户端.当这种情况发生时,URL加载类会通知其代理.如果你的app实现了对应的代理方法,你可以选择重新请求定向后的地址还是返回一个错误信息.
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共用。