NSHTTPCookie之二三事

引言:

NSHTTPCookie对象代表一个HTTP cookie。我们接下来通过系统的API来看看这个类都可以做什么。
一,初始化方法
- (nullable instancetype)initWithProperties:(NSDictionary *)properties;
+ (nullable NSHTTPCookie *)cookieWithProperties:(NSDictionary *)properties;
这两个方法的重点是传入的字典参数,这个字典key的类型是特殊的。
必须是“NSHTTPCookiePropertyKey”
typedef  NSString * NSHTTPCookiePropertyKey;

//必选项
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieName;
//必选项
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieValue;
//必选项
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookiePath;
//下面两个必选一个
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieOriginURL;
FOUNDATION_EXPORT NSHTTPCookiePropertyKey const NSHTTPCookieDomain;
//下面这些都是可选项
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”其实就是关于cookie的所有设置项。
好吧,我们来看一段初始化的代码:
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的相关属性:
//保存NSHTTPCookie的属性集
@property (nullable, readonly, copy) NSDictionary *properties;
//NSHTTPCookieName
@property (readonly, copy) NSString *name;
//NSHTTPCookieValue
@property (readonly, copy) NSString *value;
//NSHTTPCookiePath
//cookie将在cookie域中发送此路径的请求,以及所有具有此前缀的路径。“/”路径意味着cookie将被发送到域中的所有URL。
@property (readonly, copy) NSString *path;
//NSHTTPCookieVersion
// cookie的版本 0或者1
@property (readonly)       NSUInteger version;
//NSHTTPCookieExpires
// 过期时间
@property (nullable, readonly, copy)       NSDate *expiresDate;
//NSHTTPCookieDiscard
// 在会话结束的时候是否移除cookie(不考虑它的过期时间)
@property (readonly, getter=isSessionOnly) BOOL sessionOnly;
//NSHTTPCookieDomain
// cookie所属的域
@property (readonly, copy) NSString *domain;
//NSHTTPCookieComment
// cookie的注释,说明cookir的用途和目的
@property (nullable, readonly, copy)     NSString *comment;
//NSHTTPCookieCommentURL
// 注释的链接,进一步说明Cookie
@property (nullable, readonly, copy)     NSURL *commentURL;
// 是否发送给http服务器
@property (readonly, getter=isHTTPOnly)  BOOL HTTPOnly;
// cookie是否通过ssl安全链接发送
@property (readonly, getter=isSecure)    BOOL secure;
// cookies的所有端口
@property (nullable, readonly, copy) NSArray *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 *)requestHeaderFieldsWithCookies:(NSArray *)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 *)cookiesWithResponseHeaderFields:(NSDictionary *)headerFields forURL:(NSURL *)URL;

iOS HTTPCookie基本使用

你可能感兴趣的:(NSHTTPCookie之二三事)