1、作甚Cookie
Cookie是網站為了就是終端身份,保留在終端當地的用戶憑證信息。Cookie中的字段與意義由辦事端停止界說。例如,當用戶在某個網站停止了登錄操作後,辦事端會將Cookie信息前往給終端,終端會將這些信息停止保留,鄙人一次再次拜訪這個網站時,終端會將保留的Cookie信息一並發送到辦事端,辦事端依據Cookie信息能否有用來斷定此用戶能否可以主動登錄。
2、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;
3、消除Cookie
消除一切的cookie 辦法:
NSURL *url = [NSURL URLWithString:@"http://www.百度.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];
【詳解iOS App開辟中Cookie的治理辦法】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!