NSHTTPCookieStorage(Objective-C)官方文檔翻譯

繼承自:NSObject
遵守協(xié)議:NSObject
導(dǎo)入聲明:@import Foundation;
適用范圍:iOS 2.0 及以后

一胰柑、概述(Overview)

NSHTTPCookieStorage 是一個(gè)用來(lái)管理 cookie 存儲(chǔ)的單例沙咏。一個(gè) NSHTTPCookie 單例代表一個(gè) cookie食茎。通常來(lái)講丹锹,cookie 可以在應(yīng)用間共享犹赖,并且在進(jìn)程之間保持同步。 對(duì)于單進(jìn)程卷仑,Session cookies (這里的 cookie 對(duì)象的 isSessionOnly 方法返回 YES)是局部的并且不能被共享峻村。

iOS 說(shuō)明
在 iOS 中粘昨,應(yīng)用間是不能共享 cookie 的张肾。

說(shuō)明
cookie 接收策略(Cookie Accept Policy)的改變會(huì)對(duì)所有正在運(yùn)行的使用 cookie 存儲(chǔ)的應(yīng)用產(chǎn)生影響。

在 OS X 10.9 及以后和 iOS 7 及以后吞瞪,NSHTTPCookieStorage 是線程安全的芍秆。

二、功能(Tasks)

1.創(chuàng)建并初始化一個(gè) NSHTTPCookieStorage 對(duì)象(Creating and Initializing a Cookie Storage Object)
- initWithStorageLocation: Available in iOS 4.0 through iOS 4.3
2.獲取共享的 NSHTTPCookieStorage 對(duì)象(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.獲取和設(shè)置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

參數(shù) 含義
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

參數(shù) 含義
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

參數(shù) 含義
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

參數(shù) 含義
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

參數(shù) 含義
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.

三、數(shù)據(jù)類型(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


問(wèn)題(Question)

  1. NSHTTPCookieStorage 在什么場(chǎng)景下需要用到不从?
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末椿息,一起剝皮案震驚了整個(gè)濱河市坷衍,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌乏矾,老刑警劉巖迁杨,帶你破解...
    沈念sama閱讀 222,681評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異捷沸,居然都是意外死亡狐史,警方通過(guò)查閱死者的電腦和手機(jī)说墨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,205評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門尼斧,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)试吁,“玉大人,你說(shuō)我怎么就攤上這事烛恤≈位剑” “怎么了糙申?”我有些...
    開封第一講書人閱讀 169,421評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵柜裸,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我扛邑,道長(zhǎng)铐然,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,114評(píng)論 1 300
  • 正文 為了忘掉前任沥阳,我火速辦了婚禮自点,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘桂敛。我一直安慰自己,他們只是感情好薪伏,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,116評(píng)論 6 398
  • 文/花漫 我一把揭開白布毅该。 她就那樣靜靜地躺著,像睡著了一般眶掌。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上即寒,一...
    開封第一講書人閱讀 52,713評(píng)論 1 312
  • 那天母赵,我揣著相機(jī)與錄音,去河邊找鬼凹嘲。 笑死构韵,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的凶朗。 我是一名探鬼主播显拳,決...
    沈念sama閱讀 41,170評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼宛畦!你這毒婦竟也來(lái)了揍移?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,116評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤斯够,失蹤者是張志新(化名)和其女友劉穎喧锦,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體束亏,經(jīng)...
    沈念sama閱讀 46,651評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡阵具,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,714評(píng)論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了怕敬。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,865評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡畸陡,死狀恐怖丁恭,靈堂內(nèi)的尸體忽然破棺而出斋日,到底是詐尸還是另有隱情,我是刑警寧澤恶守,帶...
    沈念sama閱讀 36,527評(píng)論 5 351
  • 正文 年R本政府宣布熬的,位于F島的核電站赊级,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏橡伞。R本人自食惡果不足惜晋被,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,211評(píng)論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望挂脑。 院中可真熱鬧欲侮,春花似錦、人聲如沸刁俭。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,699評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)宪哩。三九已至第晰,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間罗岖,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,814評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工桑包, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留哑了,地道東北人烧颖。 一個(gè)月前我還...
    沈念sama閱讀 49,299評(píng)論 3 379
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像拆火,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子们镜,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,870評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容