https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/URLLoadingSystem/CookiesandCustomProtocols/CookiesandCustomProtocols.html#//apple_ref/doc/uid/10000165i-CH10-SW3

If your app needs to manage cookies programmatically, such as adding and deleting cookies or determining which cookies should be accepted, read Cookie Storage.

If your app needs to support a URL-based protocol that NSURL does not support natively, you can register your own custom protocol class that provides the needed support. To learn more, read Protocol Support.

Cookie Storage

Due to the stateless nature of the HTTP protocol, clients often use cookies to provide persistent storage of data across URL requests. The URL loading system provides interfaces to create and manage cookies, to send cookies as part of an HTTP request, and to receive cookies when interpreting a web server’s response.

The NSHTTPCookie class encapsulates a cookie, providing accessors for many of the common cookie attributes. This class also provides methods to convert HTTP cookie headers to NSHTTPCookie instances and convert an NSHTTPCookie instance to headers suitable for use with an NSURLRequest object. The URL loading system automatically sends any stored cookies appropriate for an NSURLRequest object unless the request specifies not to send cookies. Likewise, cookies returned in an NSURLResponse object are accepted in accordance with the current cookie acceptance policy.

The NSHTTPCookieStorage class provides the interface for managing the collection of NSHTTPCookie objects shared by all apps.

iOS Note: Cookies are not shared between apps in iOS.

NSHTTPCookieStorage allows an app to specify a cookie acceptance policy. The cookie acceptance policy controls whether cookies should always be accepted, never be accepted, or be accepted only from the same domain as the main document URL.

Note: Changing the cookie acceptance policy in an app affects the cookie acceptance policy for all other running apps.

When another app changes the cookie storage or the cookie acceptance policy, NSHTTPCookieStorage notifies an app by posting the NSHTTPCookieManagerCookiesChangedNotification and NSHTTPCookieStorageAcceptPolicyChangedNotification notifications.

For more information, see NSHTTPCookieStorage Class Reference and NSHTTPCookie Class Reference.

Protocol Support

The URL loading system design allows a client app to extend the protocols that are supported for transferring data. The URL loading system natively supports the httphttpsfileftp, and data protocols.

You can implement a custom protocol by subclassing NSURLProtocol and then registering the new class with the URL loading system using the NSURLProtocol class method registerClass:. When an NSURLSession object initiates a connection for an NSURLRequest object, the URL loading system consults each of the registered classes in the reverse order of their registration. The first class that returns YES for a canInitWithRequest: message is used to handle the request.

If your custom protocol requires additional properties for its requests or responses, you support them by creating categories on the NSURLRequestNSMutableURLRequest, and NSURLResponse classes that provide accessors for those properties. The NSURLProtocol class provides methods for setting and getting property values in those accessors.

The URL loading system is responsible for creating and releasing NSURLProtocol instances when connections start and complete. Your app should never create an instance of NSURLProtocol directly.

When an NSURLProtocol subclass is initialized by the URL loading system, it is provided a client object that conforms to the NSURLProtocolClientprotocol. The NSURLProtocol subclass sends messages from the NSURLProtocolClient protocol to the client object to inform the URL loading system of its actions as it creates a response, receives data, redirects to a new URL, requires authentication, and completes the load. If the custom protocol supports authentication, then it must conform to the NSURLAuthenticationChallengeSender protocol.

For more information, see NSURLProtocol Class Reference.

Cookies and Custom Protocols的更多相关文章

  1. sanic官方文档解析之Custom Protocols(自定义协议)和Socket(网络套接字)

    1,Custom Protocol:自定义协议 温馨提示:自定义协议是一个高级用法,大多数的读者不需要用到此功能 通过特殊的自定义协议,你可以改变sanic的协议,自定义协议需要继承子类asyncio ...

  2. iOS - NSURLSession 网络请求

    前言 NS_CLASS_AVAILABLE(NSURLSESSION_AVAILABLE, 7_0) @interface NSURLSession : NSObject @available(iOS ...

  3. NSURLSession 网络请求

    1.NSURLSession 在 iOS9.0 之后,以前使用的 NSURLConnection 过期,苹果推荐使用 NSURLSession 来替换 NSURLConnection 完成网路请求相关 ...

  4. Android Weekly Notes Issue #235

    Android Weekly Issue #235 December 11th, 2016 Android Weekly Issue #235 本期内容包括: 开发一个自定义View并发布为开源库的完 ...

  5. 实现SQL Server 2008 Reporting Services匿名访问报表有两种方法

    一.通过修改SQL Server 2008的配置文件,去掉Windows的验证. 1.首先我们找到SQL安装目录下的两个Web.config配置文件,默认安装目录分别是(C:\Program File ...

  6. iOS开发之网络编程--6、NSURLSessionConfiguration笔记

    NSURLSessionConfiguration对象用于初始化NSURLSession对象. 展开请求级别中与NSMutableURLRequest相关的可供选择的方案,我们可以看到NSURLSes ...

  7. SQOOP Load Data from Oracle to Hive Table

    sqoop import -D oraoop.disabled=true \ --connect "jdbc:oracle:thin:@(description=(address=(prot ...

  8. iPad accessory communication through UART

    We manufacture a new accessory for iPad/iPhone which should transfer commands to the iPad. We like t ...

  9. iOS 开发中使用 NSURLProtocol 拦截 HTTP 请求

    这篇文章会提供一种在 Cocoa 层拦截所有 HTTP 请求的方法,其实标题已经说明了拦截 HTTP 请求需要的了解的就是 NSURLProtocol. 由于文章的内容较长,会分成两部分,这篇文章介绍 ...

随机推荐

  1. java ---- 认识类和对象

    ----  开发方法 结构化开发方法特点: 1.面向功能划分软件结构 2.自顶而下 3.最小的子系统是方法 4.制约了软件的可维护性和扩展性 面向对象开发方法特点: 1.把软件系统看成各种对象和集合 ...

  2. stm32 普通IO口模拟串口通信

    普通IO口模拟串口通信 串口通信协议 串口传输 默认 波特率9600 1起始位 1停止位 其他0 数据位是8位(注意图上的给错了). 传输时,从起始位开始,从一个数据的低位(LSB)开始发送,如图从左 ...

  3. bootstrap table--面相配置、hook、适配的表格框架

    bootstrap table--面相配置.hook.适配的表格框架

  4. 近3年常考的Spring面试题及答案

    1. 一般问题 1.1. 不同版本的 Spring Framework 有哪些主要功能? Version Feature Spring 2.5 发布于 2007 年.这是第一个支持注解的版本. Spr ...

  5. mybatis插入数据后返回自增主键ID详解

    1.场景介绍: ​ 开发过程中我们经常性的会用到许多的中间表,用于数据之间的对应和关联.这个时候我们关联最多的就是ID,我们在一张表中插入数据后级联增加到关联表中.我们熟知的mybatis在插入数据后 ...

  6. [更新] Jetbrains IntelliJ IDEA 2019 的许可证密钥

    亲测有效.转载目的:提供一种方法,当然可以看一下原网站的更新及其他方法.转载:[更新] Jetbrains IntelliJ IDEA 2019 的许可证密钥(100%工作),By Gideon    ...

  7. pytest之mark功能

    pytest系列(一)中给大家介绍了pytest的特性,以及它的编写用例的简单至极. 那么在实际工作当中呢,我们要写的自动化用例会比较多,不会都放在一个py文件里. 如下图所示,我们编写的用例存放在不 ...

  8. CMS-headless or non-headless, page-based or object-based storage?

    内容管理系统对于很多在线教育企业来说都是至关重要的,他不仅可以用于内容的创作,编辑,发布,撤销,展示也可以用于运营或者市场产生他们需要的页面. 传统上,Wordpress是一个非常成功的CMS,他将内 ...

  9. WinExec, ShellExecute,CreateProcess 区别

    其中以WinExec最为简单,ShellExecute比WinExec灵活一些,CreateProcess最为复杂. WinExec 有两个参数,前一个指定路径,后一个指定显示方式. ShellExe ...

  10. Django:RestFramework之-------路由

    11.路由 路由设置: url(r'^(?P<version>[v1|v2]+)/vview\.(?P<format>\w+)$', views.VView.as_view({ ...