NSHTTPCookieStorage(Objective-C)官方文档翻译

继承自:NSObject
遵守协议:NSObject
导入声明:@import Foundation;
适用范围:iOS 2.0 及以后

一、概述(Overview)

NSHTTPCookieStorage 是一个用来管理 cookie 存储的单例。一个 NSHTTPCookie 单例代表一个 cookie。通常来讲,cookie 可以在应用间共享,并且在进程之间保持同步。 对于单进程,Session cookies (这里的 cookie 对象的 isSessionOnly 方法返回 YES)是局部的并且不能被共享。

iOS 说明
在 iOS 中,应用间是不能共享 cookie 的。

说明
cookie 接收策略(Cookie Accept Policy)的改变会对所有正在运行的使用 cookie 存储的应用产生影响。

在 OS X 10.9 及以后和 iOS 7 及以后,NSHTTPCookieStorage 是线程安全的。

二、功能(Tasks)

1.创建并初始化一个 NSHTTPCookieStorage 对象(Creating and Initializing a Cookie Storage Object)
- initWithStorageLocation: Available in iOS 4.0 through iOS 4.3
2.获取共享的 NSHTTPCookieStorage 对象(Getting the Shared Cookie Storage Object)
+ sharedHTTPCookieStorage

Returns the shared cookie storage instance.

Declaration

+ (NSHTTPCookieStorage *)sharedHTTPCookieStorage

Return Value
The shared cookie storage instance.

Availability
Available in iOS 2.0 and later.

3.获取和设置Cookie Accept Policy(Getting and Setting the Cookie Accept Policy)
cookieAcceptPolicy Property

The cookie storage’s cookie accept policy.

Declaration

@property NSHTTPCookieAcceptPolicy cookieAcceptPolicy

Discussion
The default cookie accept policy is NSHTTPCookieAcceptPolicyAlways
. Changing the cookie policy affects all currently running applications using the cookie storage.

Availability
Available in iOS 2.0 and later.

4.添加和删除 cookie(Adding and Removing Cookies)

- deleteCookie:

Deletes the specified cookie from the cookie storage.

Declaration

- (void)deleteCookie:(NSHTTPCookie *)aCookie

Parameters

参数 含义
aCookie The cookie to delete.

Availability
Available in iOS 2.0 and later.


- setCookie:

Stores a specified cookie in the cookie storage if the cookie accept policy permits.

Declaration

- (void)setCookie:(NSHTTPCookie *)aCookie

Parameters

参数 含义
aCookie The cookie to store.

Discussion
The cookie replaces an existing cookie with the same name, domain, and path, if one exists in the cookie storage. This method accepts the cookie only if the receiver’s cookie accept policy isNSHTTPCookieAcceptPolicyAlways or NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain. The cookie is ignored if the receiver’s cookie accept policy is NSHTTPCookieAcceptPolicyNever.

Availability
Available in iOS 2.0 and later.


- setCookies:forURL:mainDocumentURL:

Adds an array of cookies to the receiver if the receiver’s cookie acceptance policy permits.

Declaration

- (void)setCookies:(NSArray<NSHTTPCookie *> *)cookies
            forURL:(NSURL *)theURL
   mainDocumentURL:(NSURL *)mainDocumentURL

Parameters

参数 含义
cookies The cookies to add.
theURL The URL associated with the added cookies.
mainDocumentURL The URL of the main HTML document for the top-level frame, if known. Can be nil. This URL is used to determine if the cookie should be accepted if the cookie accept policy is NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain.

Discussion
The cookies will replace existing cookies with the same name, domain, and path, if one exists in the cookie storage. The cookie will be ignored if the receiver's cookie accept policy is NSHTTPCookieAcceptPolicyNever.
To store cookies from a set of response headers, an application can use cookiesWithResponseHeaderFields:forURL: passing a header field dictionary and then use this method to store the resulting cookies in accordance with the receiver’s cookie acceptance policy.

Availability
Available in iOS 2.0 and later.

5.读取 cookie (Retrieving Cookies)
cookies Property

The cookie storage’s cookies. (read-only)

Declaration

@property(readonly, copy) NSArray  <NSHTTPCookie *> *cookies

Discussion
If you want to sort the cookie storage’s cookies, you should use the sortedCookiesUsingDescriptors: method instead of sorting the result of this method.

Availability
Available in iOS 2.0 and later.

See Also
– cookiesForURL:


- cookiesForURL:

Returns all the cookie storage’s cookies that are sent to a specified URL.

Declaration

- (NSArray <NSHTTPCookie *> *)cookiesForURL:(NSURL *)theURL

Parameters

参数 含义
theURL The URL to filter on.

Return Value
An array of cookies whose URL matches the provided URL.

Discussion
An application can use NSHTTPCookie method requestHeaderFieldsWithCookies: to turn this array into a set of header fields to add to an NSMutableURLRequest object.

Availability
Available in iOS 2.0 and later.

See Also
– cookies


- sortedCookiesUsingDescriptors:

Returns all of the cookie storage’s cookies, sorted according to a given set of sort descriptors.

Declaration

- (NSArray)<NSHTTPCookie *> *)sortedCookiesUsingDescriptors:(NSArray <NSSortDescriptor *> *)sortOrder

Parameters

参数 含义
sortOrder The sort descriptors to use for sorting, as an array of NSSortDescriptor objects.

Return Value
The cookie storage’s cookies, sorted according to sortOrder
, as an array of NSHTTPCookie objects.

Availability
Available in iOS 5.0 and later.

三、数据类型(Data Types)

NSHTTPCookieAcceptPolicy

specifies the cookie acceptance policies implemented by theNSHTTPCookieStorage
class.

Declaration

typedef enum {
   NSHTTPCookieAcceptPolicyAlways,                     // Accept all cookies. This is the default cookie accept policy. Available in iOS 2.0 and later.
   NSHTTPCookieAcceptPolicyNever,                      // Reject all cookies. Available in iOS 2.0 and later.
   NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain  // Accept cookies only from the main document domain. Available in iOS 2.0 and later.
} NSHTTPCookieAcceptPolicy;

四、通知(Notifications)

NSHTTPCookieManagerCookiesChangedNotification

This notification is posted when the cookies stored in the NSHTTPCookieStorage instance have changed.
In OS X, cookies are shared among applications, meaning this notification can be sent in response to another application’s actions. Cookies are not shared among applications in iOS.
The notification object is the NSHTTPCookieStorage instance. This notification does not contain a userInfo dictionary.

NSHTTPCookieManagerAcceptPolicyChangedNotification

This notification is posted when the acceptance policy of the NSHTTPCookieStorage instance has changed.
In OS X, cookies are shared among applications, meaning this notification can be sent in response to another application’s actions. Cookies are not shared among applications in iOS.
The notification object is the NSHTTPCookieStorage instance. This notification does not contain a userInfo dictionary.

参考(Reference)

https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSHTTPCookieStorage_Class/#//apple_ref/c/data/NSHTTPCookieManagerAcceptPolicyChangedNotification


问题(Question)

  1. NSHTTPCookieStorage 在什么场景下需要用到?
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容