Cookies and Custom Protocols
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.
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 http
, https
, file
, ftp
, 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 NSURLRequest
, NSMutableURLRequest
, 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 NSURLProtocolClient
protocol. 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.
相关文章
- AngularJS学习---REST和自定义服务(REST and Custom Services) ngResource step 11
- [Angular 8] Custom Route Preloading with ngx-quicklink and Angular
- [Angular] Http Custom Headers and RequestOptions
- [Angular] Create custom validators for formControl and formGroup
- [TypeScript] Reflection and Decorator Metadata
- [RxJS] Transformation operator: map and mapTo
- 网络号 = IP和子网掩码的每位数AND 主机号 掩码取反与IP地址and运算
- Gradle 翻译 tips and recipes 使用技巧 [MD]
- AngularJS学习---REST和自定义服务(REST and Custom Services) ngResource step 11
- [React Testing] Test a Custom React Hook with React’s Act Utility and a Test Component
- [Custom CLI] Develop and Publish a Node.js CLI from Scratch
- [GraphQL] Query Local and Remote Data in Apollo Link State
- [Angular] Updating and resetting FormGroups and FormControls
- [Practical Git] Switching between current branch and last checkout branch
- Step by step to create web service in Cloud Application studio and bind it to a custom action in sta
- custom configuration DB and creation logic
- custom field further usage - add into UI and report
- 【Codeforces Round #635 (Div. 2) A】Ichihime and Triangle
- Important table for launchpad Designer and configuration
- Sysdig: Designing a Secure Container Orchestration and Containerized Architecture Strategy
- 论文解读(GCNII)《Simple and Deep Graph Convolutional Networks》
- 论文解读(ChebyGIN)《Understanding Attention and Generalization in Graph Neural Networks》
- HDU 3639 Hawk-and-Chicken(良好的沟通)
- 第二周 自然语言处理与词嵌入(Natural Language Processing and Word Embeddings)