URL Loading System Programing Guid (1)

来源:互联网 发布:英文翻译软件 编辑:程序博客网 时间:2024/05/18 03:34

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.


这个指南描述了通过标准的互联网协议与URLs交互以及和服务器通信相关的类。这些类被成为URLs加载系统。


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 theNSURL class, which lets your app manipulate URLs and the resources they refer to.

URL加载系统是一系列类和协议的集合,有了这些,你可以通过一个URL获取内容。核心的是NSURL 类,通过它可以操作URLs和你引用的内容。


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类,基础框架提供了丰富的类让你加载URL 的内容,上传数据到服务器,管理cookie存储,控制响应缓存,以app特有的方式处理证书存储和认证,以及编写自定义的协议扩张。


The URL loading system provides support for accessing resources using the following protocols:

  • File Transfer Protocol (ftp://)
  • Hypertext Transfer Protocol (http://)
  • Hypertext Transfer Protocol with encryption (https://)
  • Local file URLs (file:///)
  • Data URLs (data://)

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网关获取资源。

Important: In addition to the URL loading system, OS X and iOS provide APIs for opening URLs in other applications, such as Safari. These APIs are not described in this document.

For more information about Launch Services in OS X, read Launch Services Programming Guide.

For more information about the openURL: method in theNSWorkSpace class in OS X, read NSWorkspace Class Reference.

For more information about the openURL: method in the UIApplication class in iOS, readUIApplication Class Reference.


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.

The URL loading system class hierarchy

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 in many ways, depending on your app’s requirements. The API you choose depends on the version of OS X or iOS your app targets and whether you wish to obtain the data as a file or an in-memory block of data:

最常使用的类允许你的app从source中获取一个URL的内容。你可以通过很多方式获取内容,具体具体取决于你的app的需要。你根据系统的版本以及你希望获取数据作为一个文件还是一个内存块数据。

  • In iOS 7 and later or OS X v10.9 and later, NSURLSession is the preferred API for new code that performs URL requests.

  • For software that must support older versions of OS X, you can use NSURLDownload to download the contents of a URL to a file on disk.

  • For software that must support older versions of iOS or OS X, you can use NSURLConnection to download the contents of a URL to memory. You can then write the data to disk if needed.

1,iOS7以后, 推荐使用NSURLSession执行URL请求。

2,对于支持旧版本的OS X不了解了。

3,对于支持旧版本的iOS或者OS X,你可以使用NSURLConnection去下载内容到内存。如果需要可以之后把数据写到磁盘。

The specific methods you use depend largely on whether you wish to fetch data to memory or download it to disk.

你选择的具体的方法很大程度上取决于你想要抓取数据到内存还是要内存。

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 or NSURLConnection.

Regardless of which approach you choose, your app can obtain the response data in two ways:

不管你选择那种方法,你的app以两种方式获得响应数据

  • 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.

1,提供一个完成处理块。url 加载类会在完成获取数据后调用那个块

2,提供一个自定义的代理。当收到数据时,URL加载类会定时调用你的代理方法。你的应用负责积累数据。

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.

除了数据本身,URL加载类还给你的代理或者完成处理块提供响应对象,它封装了和请求相关的元数据,比如MIME类型,内容长度。

Relevant Chapters: “Using NSURLSession,” “Using NSURLConnection”


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. It also specifies the policy regarding the use of any locally cached data, and when used with NSURLConnection or NSURLDownload, provides an interface to set the connection timeout. (For NSURLSession, timeouts are configured on a per-session basis.)

NSURLRequest 对象以协议无关的方式封装URL和任意协议相关的属性,

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 toNSURLRequest that return the HTTP request body, headers, and transfer method. It also adds methods toNSMutableURLRequest to set those values.

The details of working with URL request objects are described throughout this book.

Response Metadata

The response from a server to a request can be viewed as two parts: metadata describing the contents and the content data itself. Metadata that is common to most protocols is encapsulated by the NSURLResponse class and consists of the MIME type, expected content length, text encoding (where applicable), and the URL that provided the response. Protocol-specific subclasses of NSURLResponse can provide additional metadata. For example, NSHTTPURLResponse stores the headers and the status code returned by the web server.

从服务器得到的响应可以分为两个部分:描述内容的元数据和内容本身。

Important: Only the metadata for the response is stored in an NSURLResponse object. The various URL loading classes provide the response data itself to your app either through a completion handler block or to the object’s delegate.

An NSCachedURLResponse instance encapsulates an NSURLResponse object, the URL content data, and any additional information provided by your app. See “Cache Management” for details.

The details of working with URL response objects are described throughout this book.





0 0