NSURLSession

NSURLSession

An object that coordinates a group of related network data transfer tasks.(翻译:协调一组相关网络数据传输任务的对象)

Overview(概观)

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.

   翻译:NSURLSession类和相关类提供用于下载内容的API。该API为支持身份验证提供了丰富的代理方法,并使您的app能够在应用程序Not Running时(Not Runnin是指 应用还没有启动,或者应用正在运行但是途中被系统停止)或者Suspended(Suspended是指应用处在后台,并且已停止执行代码。系统自动的将应用移入此状态,且在此举之前不会对应用做任何通知。当处在此状态时,应用依然驻留内存但不执行任何程序代码。当系统发生低内存告警时,系统将会将处于Suspended状态的应用清除出内存以为正在前台运行的应用提供足够的内存)时执行后台下载。

The NSURLSession class natively supports the data, file, ftp, http, and https URL schemes, with transparent support for proxy servers and SOCKS gateways, as configured in the user’s system preferences

 翻译: NSURLSession类本身支持data,file,ftp,http和https URL schemes,支持代理服务器和SOCKS网关,如在用户的系统偏好中配置的那样。

NSURLSession supports the HTTP/1.1, SPDY, and HTTP/2 protocols. HTTP/2 support is described by RFC 7540, and requires a server supporting either ALPN or NPN for protocol negotiation.

 翻译:NSURLSession支持HTTP / 1.1,SPDY和HTTP / 2协议。 HTTP / 2的描述由RFC 7540描述的,且还需要一台支持ALPN或NPN协议的服务器.

You can also add support for your own custom networking protocols and URL schemes (for your app’s private use) using NSURLProtocol.

翻译:您还可以使用NSURLProtocol来实现自己的自定义网络协议和URL schemes(为您的应用程序的私人使用)

Important(重要)

The NSURLSession API involves many different classes working together in a fairly complex way that may not be obvious if you read the reference documentation by itself. Before using this API, you should read URL Session Programming Guide to gain a conceptual understanding of how these classes interact with one another.

翻译:NSURLSession API涉及许多不同的类,如果您单独阅读NSURLSession参考文档的话,可能不能很明显的了解到它们相当复杂的工作方式,所以此API之前,您应该阅读*URL Session Programming Guide*,以获得对这些类如何相互交互的概念性了解.

With the NSURLSession API, your app creates one or more sessions, each of which coordinates a group of related data transfer tasks. For example, if you are writing a web browser, your app might create one session per tab or window, or one session for interactive use and another session for background downloads. Within each session, your app adds a series of tasks, each of which represents a request for a specific URL (following HTTP redirects if necessary).

翻译:要使用NSURLSession API,您的应用程序会创建一个或多个会话,每个会话协调一组相关的数据传输任务。例如,如果您正在编写Web浏览器,则应用程序可能会在每个选项卡或窗口中创建一个会话,或一个会话用于交互式使用,另一个会话用于后台下载。在每个会话中,您的应用程序会添加一系列任务,每个任务代表对特定URL的请求(如果原始URL返回HTTP重定向,则会显示任何后续URL)。

The tasks within a given URL session share a common session configuration object, which defines connection behavior, such as the maximum number of simultaneous connections to make to a single host, whether to allow connections over a cellular network, and so on. The behavior of a session is determined in part by which method you call when creating its configuration object:

翻译:给定的URL会话的任务共享一个公共会话配置对象,该对象定义连接行为,例如对单个主机进行的并发连接的最大数量,是否允许通过蜂窝网络进行连接等等.一个会话的行为在某种程度上取决于你调用哪个方法在创建它的配置对象:

<1> The singleton shared session (which has no configuration object) is for basic requests. It is not as customizable as sessions that you create, but it serves as a good starting point if you have very limited requirements. You access this session by calling the
sharedSession class method. See that method’s discussion for more information about its limitations.

翻译:sharedSession单例共享会话(没有配置对象)用于基本请求。不可以自定义,如果您的需求非常有限,它可以作为一个好的起点。您通过调用访问sharedSession 类方法得到一个共享的单例会话对象NSURLSession。有关其限制的更多信息,请参阅该方法的讨论。

<2>Default sessions behave much like the shared session (unless you customize them further), but let you obtain data incrementally using a delegate. You can create a default session configuration by calling the defaultSessionConfiguration method on the
NSURLSessionConfiguration class.

翻译:默认会话类似于共享会话(除非您进一步自定义),但是默认会话可以使用委托逐步获取数据。 您可以通过调用NSURLSessionConfiguration类中的defaultSessionConfiguration创建默认会话配置.

<3>Ephemeral sessions are similar to default sessions, but do not write caches, cookies, or credentials to disk. You can create an ephemeral session configuration by calling the
ephemeralSessionConfiguration method on the NSURLSessionConfiguration class.

翻译:短暂会话类似于默认会话,但是它不要将缓存,cookie或凭据写入磁盘。您可以通过NSURLSessionConfiguration类,调用ephemeralSessionConfiguration创建一个短暂的会话配置.

Background sessions let you perform uploads and downloads of content in the background while your app is not running. You can create a background session configuration by calling the
backgroundSessionConfiguration:
method on the NSURLSessionConfiguration class.

翻译:后台会话可让您在应用程序未运行时在后台执行内容的上传和下载。您可以通过NSURLSessionConfiguration类调用backgroundSessionConfiguration来创建后台会话配置.

The session configuration object also contains a reference to URL cache and cookie storage objects that may be used when making requests and handling the responses, depending on the configuration and request type

翻译:会话配置对象还包含对根据配置和请求类型进行请求和处理响应时可能使用的URL缓存和Cookie存储对象的引用。

The tasks in a session also share a common delegate that lets you provide and obtain information when various events occur—when authentication fails, when data arrives from the server, when data is ready to be cached, and so on. For all background downloads and uploads, you must provide a delegate that conforms to the
NSURLSessionDownloadDelegate
Objective-C protocol. Otherwise, if you don’t need any of the features provided by a delegate, you can use this API without providing a delegate by passing nil when you create a session.

翻译:会话中的任务还会共享一个通用委托,允许您在发生各种事件时提供和获取信息,当身份验证失败时,数据从服务器到达,数据准备好被缓存时,等等。对于所有后台下载和上传,您必须提供一个符合的代理
NSURLSessionDownloadDelegate Objective-C协议。 否则,如果您不需要委托提供的任何功能,您可以在创建会话时通过传递nil而不提供委托来使用此API.

Important
The session object keeps a strong reference to the delegate until your app exits or explicitly invalidates the session. If you do not invalidate the session, your app leaks memory until it exits.

翻译:会话对象保持对代理的强烈引用,直到您的应用程序退出或显式地使会话无效。如果您不使会话无效,则您的应用程序会在内存中泄漏,直到它退出.

Within a session, you create tasks that optionally upload data to a server, then retrieve data from the serve***r either ***as a file on disk or as one or more NSData objects in memory. The NSURLSession API provides three types of tasks:

翻译:在会话中,您创建一个选择将数据上传到服务器的任务,然后从服务器中检索数据,检索到的数据要么作为磁盘上的文件,要么作为内存中的一个或多个NSData对象。NSURLSession API提供三种类型的任务:

<1>Data tasks send and receive data using NSData objects. Data tasks are intended for short, often interactive requests to a server.
<2>.Upload tasks are similar to data tasks, but they also send data (often in the form of a file), and support background uploads while the app is not running.
<3>.Download tasks retrieve data in the form of a file, and support background downloads and uploads while the app is not running.
<1>.数据任务使用数据发送和接收数据 的NSData对象。 数据任务旨在简化,通常是交互式的服务器请求.
<2>.上传任务与数据任务类似,但它们也会发送数据(通常以文件的形式),并且在应用程序未运行时支持后台上传.
<3>.下载任务以文件的形式检索数据,并在应用程序未运行时支持后台下载和上传.
Like most networking APIs, the NSURLSession API is highly asynchronous. It returns data to your app in one of two ways, depending on the methods you call:

翻译:像大多数网络API一样,NSURLSession API是高度异步的。 根据您调用的方法,可以通过以下两种方式之一将数据返回到您的应用程序:

<1>To acompletion handler block that is called when a transfer finishes successfully or with an error.
<2>. By calling methods on the session’s delegate as data is received and when the transfer is complete.

 <1>当传输成功完成或出现错误时调用完成处理的方法将数据返回到您的应用程序。
 <2>通过在接收到数据和传输完成时调用session的代理方法将数据返回到您的应用程序。

In addition to delivering this information to delegates, the NSURLSession API provides status and progress properties that you can query if you need to make programmatic decisions based on the current state of the task (with the caveat that its state can change at any time).

URL sessions also support canceling, restarting or resuming, and suspending tasks, and provide the ability to resume suspended, canceled, or failed downloads where they left off.

翻译:除了将此信息提供给代表外,NSURLSession API还提供了可以查询的状态和进度属性,如果您需要根据任务的当前状态进行编程决策(随时注意其状态可随时更改).
URL会话还支持 取消,重新启动 或 恢复 和 挂起任务,并提供 恢复停止,取消 或 失败的下载的 能力。

URL Session Class Hierarchy

URL会话类层次结构:

这里写图片描述

The NSURLSession API consists of the following classes (nested to show subclass relationships): 翻译:NSURLSession API由以下类(嵌套以显示子类关系)组成:

<1>NSURLSession  —A session object(会话对象).

这里写图片描述

 <2>NSURLSessionConfiguration —A configuration object used when initializing the session(初始化会话时使用的配置对象,NSURLSessionConfiguration为NSURLSession配置一些请求所需要的策略。如:超时、缓存策略、链接需求的).

<3>NSURLSessionTask  —The base class for tasks within a session(会话中任务的基类).

    1>NSURLSessionDataTask  —A task for retrieving the contents of a URL as an NSData object(将URL的内容作为NSData对象检索的任务).

        ①NSURLSessionUploadTask  —A task for uploading a file, then retrieving the contents of a URL as an NSData object(一个用于上传文件,然后检索URL的内容作为NSData对象的任务).

    2>NSURLSessionDownloadTask  —A task for retrieving the contents of a URL as a temporary file on disk(将URL的内容检索为磁盘上的临时文件的任务).

    3>NSURLSessionStreamTask  —A task for establishing a TCP/IP connection(一个建立TCP / IP连接的任务).

In addition, the NSURLSession API provides four protocols that define delegate methods your app can implement to provide more fine-grained control over session and task behavior(翻译:此外,NSURLSession API提供了四种协议,可以定义您的应用程序可以实现的委托方法,以便对会话和任务行为提供更细粒度的控制。官方文档写的提供四种,不知道为什么给出了五中协议方法).

<1>NSURLSessionDelegate  —Defines delegate methods to handle session-level events(定义代理方法来处理会话级事件).

<2>NSURLSessionTaskDelegate   —Defines delegate methods to handle task-level events common to all task types(定义代理方法来处理所有任务类型通用的任务级别事件).

<3>NSURLSessionDataDelegate  —Defines delegate methods to handle task-level events specific to data and upload tasks(定义代理方法来处理特定于数据和上传任务的任务级事件).

<4>NSURLSessionDownloadDelegate  —Defines delegate methods to handle task-level events specific to download tasks(定义代理方法来处理特定于下载任务的任务级事件).

<5>NSURLSessionStreamDelegate —Defines delegate methods to handle task-level events specific to stream tasks(定义代理方法来处理特定于流任务的任务级事件).

Finally, the NSURLSession API uses a number of classes that are also commonly used with other APIs such as NSURLConnection and NSURLDownload. Some of these shared classes include:(翻译:最后,NSURLSession API使用了很多类,它们也常用于其他API,如NSURLConnection和NSURLDownload。其中一些共享类包括:NSURL,NSURLRequest,NSURLResponse,NSCacheURLResponse)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值