NSHTTPCookie
一个NSHTTPCookie对象就代表了一个HTTP cookie. cookie有两个版本:
- Version 0: This version refers to “traditional” or “old-style” cookies, the original cookie format defined by Netscape. The majority of cookies encountered are in this format.
- Version 1: This version refers to cookies as defined in RFC 2965, HTTP State Management Mechanism.
我们可以通过NSHTTPCookie访问cookie的各个属性,它也提供了将HTTP cookie headers转换为NSHTTPCookie实例对象以及将NSHTTPCookie对象转化为对应NSURLRequest请求头的方法. URL loading system 会自动的发送已存储的对应NSURLRequest的cookies. - 创建NSHTTPCookie:
NSMutableDictionary *properties = [NSMutableDictionary dictionary];
[properties setObject:key forKey:NSHTTPCookieName];
[properties setObject:newValue forKey:NSHTTPCookieValue];
[properties setObject:domian forKey:NSHTTPCookieDomain];
[properties setObject:path forKey:NSHTTPCookiePath];
NSHTTPCookie *cookieuser = [NSHTTPCookie
cookieWithProperties:properties];
有几个key是必须要设置的: NSHTTPCookiePath, NSHTTPCookieName, NSHTTPCookieValue,还有就是NSHTTPCookieOriginURL 和 NSHTTPCookieDomain一定要选一个.
NSHTTPCookieStorage
NSHTTPCookieStorage是一个单例,用来管理存储的cookies.我们可以通过NSHTTPCookieStorage来设置cookie的接收策略,有三种模式: NSHTTPCookieAcceptPolicyAlways(默认),
NSHTTPCookieAcceptPolicyNever,
NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain.
Cookies在iOS中是不能共享的,但在Mac OS是可以的(Session cookies 不能共享).在Mac OS app中更改cookie的接收策略会影响到其他正在运行的在使用cookie storage的app.这时NSHTTPCookieStorage会发出两个通知:NSHTTPCookieManagerCookiesChangedNotification 和 NSHTTPCookieStorageAcceptPolicyChangedNotification.
- NSHTTPCookieStorage 常用API
//获取所有cookies
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [cookieStorage cookies]) {
NSLog(@"%@", cookie);
}
//删除cookies
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *tempArray = [NSArray arrayWithArray:[cookieStorage cookies]];
for (NSHTTPCookie *cookiej in tempArray) {
[cookieStorage deleteCookie:cookie];
}
//给指定的url设置cookies
-(void)addCookieToURL:(NSURL *)url key:(NSString *)key value:(NSString *)value{
NSString *newValue = (value != nil) ? value : @"";
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSMutableArray *cookies = [NSMutableArray arrayWithArray:[cookieStorage cookiesForURL:url]];
NSString *domian = [url host];
NSString *path = [url path];
NSMutableArray *tempCookies = [cookies copy];
for (NSHTTPCookie *cookie in tempCookies) {
if ([cookie.name isEqualToString:key]) {
[cookies removeObject:cookie];
}
}
NSMutableDictionary *properties = [NSMutableDictionary dictionary];
[properties setObject:key forKey:NSHTTPCookieName];
[properties setObject:newValue forKey:NSHTTPCookieValue];
[properties setObject:domian forKey:NSHTTPCookieDomain];
[properties setObject:path forKey:NSHTTPCookiePath];
NSHTTPCookie *cookieuser = [NSHTTPCookie cookieWithProperties:properties];
[cookies addObject:cookieuser];
/*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.
*/
//- (void)setCookie:(NSHTTPCookie *)aCookie
[cookieStorage setCookies:cookies forURL:url mainDocumentURL:nil];
}