一、何為Cookie
Cookie是網站為了便是終端身份,保存在終端本地的用戶憑證信息。Cookie中的字段與意義由服務端進行定義。例如,當用戶在某個網站進行了登錄操作後,服務端會將Cookie信息返回給終端,終端會將這些信息進行保存,在下一次再次訪問這個網站時,終端會將保存的Cookie信息一並發送到服務端,服務端根據Cookie信息是否有效來判斷此用戶是否可以自動登錄。
二、iOS中進行Cookie管理的兩個類
iOS中進行HTTP網絡請求Cookie管理主要由兩個類負責,一個類是NSHTTPCookieStorage類,一個是NSHTTPCookie類。
1.NSHTTPCookieStorage
NSHTTPCookieStorage類采用單例的設計模式,其中管理著所有HTTP請求的Cookie信息,常用方法如下:
//獲取單例對象
系統下面的兩個通知與Cookie管理有關:
+ (NSHTTPCookieStorage *)sharedHTTPCookieStorage;
//所有Cookie數據數組 其中存放NSHTTPCookie對象
@property (nullable , readonly, copy) NSArray<NSHTTPCookie *> *cookies;
//手動設置一條Cookie數據
- (void)setCookie:(NSHTTPCookie *)cookie;
//刪除某條Cookie信息
- (void)deleteCookie:(NSHTTPCookie *)cookie;
//刪除某個時間後的所有Cookie信息 iOS8後可用
- (nullable NSArray<NSHTTPCookie *> *)cookiesForURL:(NSURL *)URL;
//獲取某個特定URL的所有Cookie數據
- (void)removeCookiesSinceDate:(NSDate *)date NS_AVAILABLE(10_10, 8_0);
//為某個特定的URL設置Cookie
- (void)setCookies:(NSArray<NSHTTPCookie *> *)cookies forURL:(nullable NSURL *)URL mainDocumentURL:(nullable NSURL *)mainDocumentURL;
//Cookie數據的接收協議
/*
枚舉如下:
typedef NS_ENUM(NSUInteger, NSHTTPCookieAcceptPolicy) {
NSHTTPCookieAcceptPolicyAlways,//接收所有Cookie信息
NSHTTPCookieAcceptPolicyNever,//不接收所有Cookie信息
NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain//只接收主文檔域的Cookie信息
};
*/
@property NSHTTPCookieAcceptPolicy cookieAcceptPolicy;
//Cookie數據的接收協議改變時發送的通知
FOUNDATION_EXPORT NSString * const NSHTTPCookieManagerAcceptPolicyChangedNotification;
//管理的Cookie數據發生變化時發送的通知
FOUNDATION_EXPORT NSString * const NSHTTPCookieManagerCookiesChangedNotification;
2.NSHTTPCookie
NSHTTPCookie是具體的HTTP請求Cookie數據對象,其中屬性方法如下:
//下面兩個方法用於對象的創建和初始化 都是通過字典進行鍵值設置
- (nullable instancetype)initWithProperties:(NSDictionary<NSString *, id> *)properties;
+ (nullable NSHTTPCookie *)cookieWithProperties:(NSDictionary<NSString *, id> *)properties;
//返回Cookie數據中可用於添加HTTP頭字段的字典
+ (NSDictionary<NSString *, NSString *> *)requestHeaderFieldsWithCookies:(NSArray<NSHTTPCookie *> *)cookies;
//從指定的響應頭和URL地址中解析出Cookie數據
+ (NSArray<NSHTTPCookie *> *)cookiesWithResponseHeaderFields:(NSDictionary<NSString *, NSString *> *)headerFields forURL:(NSURL *)URL;
//Cookie數據中的屬性字典
@property (nullable, readonly, copy) NSDictionary<NSString *, id> *properties;
//請求響應的版本
@property (readonly) NSUInteger version;
//請求相應的名稱
@property (readonly, copy) NSString *name;
//請求相應的值
@property (readonly, copy) NSString *value;
//過期時間
@property (nullable, readonly, copy) NSDate *expiresDate;
//請求的域名
@property (readonly, copy) NSString *domain;
//請求的路徑
@property (readonly, copy) NSString *path;
//是否是安全傳輸
@property (readonly, getter=isSecure) BOOL secure;
//是否只發送HTTP的服務
@property (readonly, getter=isHTTPOnly) BOOL HTTPOnly;
//響應的文檔
@property (nullable, readonly, copy) NSString *comment;
//相應的文檔URL
@property (nullable, readonly, copy) NSURL *commentURL;
//服務端口列表
@property (nullable, readonly, copy) NSArray<NSNumber *> *portList;
三、清除Cookie
清除所有的cookie 方法:
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
清除某一個特定的cookie方法:
if (url) {
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:url];
for (int i = 0; i < [cookies count]; i++) {
NSHTTPCookie *cookie = (NSHTTPCookie *)[cookies objectAtIndex:i];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
NSArray * cookArray = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:self.loadURL]]; NSString * successCode = @""; for (NSHTTPCookie*cookie in cookArray) { if ([cookie.name isEqualToString:@"cookiename"]) { [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie]; } }
清除某一個url緩存的方法:
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:[NSURLRequest requestWithURL:url]];
清除所有緩存方法:
[[NSURLCache sharedURLCache] removeAllCachedResponses];