引言:
NSHTTPCookie對象代表一個HTTP cookie。我們接下來通過系統(tǒng)的API來看看這個類都可以做什么。
一,初始化方法
- (nullable instancetype)initWithProperties:(NSDictionary<NSHTTPCookiePropertyKey, id> *)properties;
+ (nullable NSHTTPCookie *)cookieWithProperties:(NSDictionary<NSHTTPCookiePropertyKey, id> *)properties;
這兩個方法的重點(diǎn)是傳入的字典參數(shù),這個字典key的類型是特殊的。
必須是“NSHTTPCookiePropertyKey”
typedef NSString * NSHTTPCookiePropertyKey;
//必選項(xiàng)
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieName;
//必選項(xiàng)
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieValue;
//必選項(xiàng)
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookiePath;
//下面兩個必選一個
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieOriginURL;
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieDomain;
//下面這些都是可選項(xiàng)
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieVersion;
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieSecure;
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieExpires;
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieComment;
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieCommentURL;
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieDiscard;
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieMaximumAge;
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookiePort;
不用過多的注釋大家也可以看懂吧。這個“ NSHTTPCookiePropertyKey”其實(shí)就是關(guān)于cookie的所有設(shè)置項(xiàng)。
好吧,我們來看一段初始化的代碼:
NSMutableDictionary *properties = [NSMutableDictionary dictionary];
[properties setObject:key forKey:NSHTTPCookieName];
[properties setObject:newValue forKey:NSHTTPCookieValue];
[properties setObject:path forKey:NSHTTPCookiePath];
[properties setObject:domian forKey:NSHTTPCookieDomain];
NSHTTPCookie *cookieuser = [NSHTTPCookie cookieWithProperties:properties];
//或者
//NSHTTPCookie *cookieuser = [[NSHTTPCookie alloc]initWithProperties:properties]
二,存儲cookie的相關(guān)屬性:
//保存NSHTTPCookie的屬性集
@property (nullable, readonly, copy) NSDictionary<NSHTTPCookiePropertyKey, id> *properties;
//NSHTTPCookieName
@property (readonly, copy) NSString *name;
//NSHTTPCookieValue
@property (readonly, copy) NSString *value;
//NSHTTPCookiePath
//cookie將在cookie域中發(fā)送此路徑的請求,以及所有具有此前綴的路徑?!?”路徑意味著cookie將被發(fā)送到域中的所有URL。
@property (readonly, copy) NSString *path;
//NSHTTPCookieVersion
// cookie的版本 0或者1
@property (readonly) NSUInteger version;
//NSHTTPCookieExpires
// 過期時(shí)間
@property (nullable, readonly, copy) NSDate *expiresDate;
//NSHTTPCookieDiscard
// 在會話結(jié)束的時(shí)候是否移除cookie(不考慮它的過期時(shí)間)
@property (readonly, getter=isSessionOnly) BOOL sessionOnly;
//NSHTTPCookieDomain
// cookie所屬的域
@property (readonly, copy) NSString *domain;
//NSHTTPCookieComment
// cookie的注釋,說明cookir的用途和目的
@property (nullable, readonly, copy) NSString *comment;
//NSHTTPCookieCommentURL
// 注釋的鏈接,進(jìn)一步說明Cookie
@property (nullable, readonly, copy) NSURL *commentURL;
// 是否發(fā)送給http服務(wù)器
@property (readonly, getter=isHTTPOnly) BOOL HTTPOnly;
// cookie是否通過ssl安全鏈接發(fā)送
@property (readonly, getter=isSecure) BOOL secure;
// cookies的所有端口
@property (nullable, readonly, copy) NSArray<NSNumber *> *portList;
/*!
@method requestHeaderFieldsWithCookies:
@abstract Return a dictionary of header fields that can be used to add the
specified cookies to the request.
@param cookies The cookies to turn into request headers.
@result An NSDictionary where the keys are header field names, and the values
are the corresponding header field values.
*/
+ (NSDictionary<NSString *, NSString *> *)requestHeaderFieldsWithCookies:(NSArray<NSHTTPCookie *> *)cookies;
/*!
@method cookiesWithResponseHeaderFields:forURL:
@abstract Return an array of cookies parsed from the specified response header fields and URL.
@param headerFields The response header fields to check for cookies.
@param URL The URL that the cookies came from - relevant to how the cookies are interpeted.
@result An NSArray of NSHTTPCookie objects
@discussion This method will ignore irrelevant header fields so
you can pass a dictionary containing data other than cookie data.
*/
+ (NSArray<NSHTTPCookie *> *)cookiesWithResponseHeaderFields:(NSDictionary<NSString *, NSString *> *)headerFields forURL:(NSURL *)URL;
iOS HTTPCookie基本使用
最后編輯于 :2018.02.07 11:52:19
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者 【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。 平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。