繼承自: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)
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.
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:
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)
問(wèn)題(Question)
- NSHTTPCookieStorage 在什么場(chǎng)景下需要用到不从?