读取和设置cookie的解决方案
实际上AFNetworking中并没有专门针对cookie封装的代码,但是由于它底层使用的是NSURLRequest,所以可以通过NSHTTPCookieStorage来操作cookie。
并且通过webview的loadRequest方法load NSURLRequest时可以共用cookie。
参考文献:
这篇文章写的比较详细,叙述了服务端以及IOS客户端的Cookie操作。
【IOS学习】网络请求中的cookie
iOS平台下cookie的使用
iOS中的cookie简介和其中的坑
iOS开发--http请求设置Cookie
iOS的Cookie存取看我绝对够!!(包含了 UIwebView AFnetworking WKwebView)
这里简述下客户端(IOS端)COOKIE的基本操作:
获取客户端存储的cookie
通过NSHTTPCookieStorage的单例类就可以获取到之前服务端的cookie
//获取本地cookie
NSHTTPCookieStorage *httpCookiesStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSDictionary *cookies = [httpCookiesStorage cookiesForURL:[NSURL URLWithString:@"http://localhost/"]];
for (NSHTTPCookie *cookie in cookies) {
NSLog(@"cookie:%@",cookie);
}
客户端设置本地cookie
//客户端设置cookie
-(void)clientSetCookie{
NSDictionary *prop1 = [NSDictionary dictionaryWithObjectsAndKeys:
@"a",NSHTTPCookieName,
@"aaa",NSHTTPCookieValue,
@"/",NSHTTPCookiePath,
[NSURL URLWithString:@"http://localhost/"],NSHTTPCookieOriginURL,
[NSDate dateWithTimeIntervalSinceNow:60],NSHTTPCookieExpires,
nil];
NSDictionary *prop2 = [NSDictionary dictionaryWithObjectsAndKeys:
@"b",NSHTTPCookieName,
@"bbb",NSHTTPCookieValue,
@"/",NSHTTPCookiePath,
[NSURL URLWithString:@"http://localhost/"],NSHTTPCookieOriginURL,
[NSDate dateWithTimeIntervalSinceNow:60],NSHTTPCookieExpires,
nil];
NSHTTPCookie *cookie1 = [NSHTTPCookie cookieWithProperties:prop1];
NSHTTPCookie *cookie2 = [NSHTTPCookie cookieWithProperties:prop2];
//单个设置
// [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie1];
// [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie2];
//批量设置
NSArray *cookies = @[cookie1,cookie2];
[[NSHTTPCookieStorage sharedHTTPCookieStorage]setCookies:cookies forURL:[NSURL URLWithString:@"http://localhost/"] mainDocumentURL:nil];
NSLog(@"设置完成");
}
说明:
- 设置好了之后,下次请求url时会自动带入cookie中的数据
- 可以一个个设置也可以使用setCookies批量设置
删除cookie
- (void)deleteCookie:(NSHTTPCookie *)cookie;
- (void)removeCookiesSinceDate:(NSDate *)date NS_AVAILABLE(10_10, 8_0);
我们来示范如何删除cookie
#pragma mark -客户端删除cookie
//根据url和name删除cookie
-(void)deleteCookie:(NSString *)cookieName url:(NSURL *)url{
//根据url找到所属的cookie集合
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage]cookiesForURL:url];
for (NSHTTPCookie *cookie in cookies) {
if([cookie.name isEqualToString:cookieName]){
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
NSLog(@"删除cookie:%@",cookieName);
}
}
}
//删除全部cookies
-(void)deleteCookies{
for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage]cookies]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
NSLog(@"删除完成");
}
cookie的本地缓存策略
//设置cookie本地缓存策略
//NSHTTPCookieAcceptPolicyAlways:保存所有cookie,这个是默认值
//NSHTTPCookieAcceptPolicyNever:不保存任何响应头中的cookie
//NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain:只保存域请求匹配的cookie
[[NSHTTPCookieStorage sharedHTTPCookieStorage]setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyNever];
自动登录(Cookie实现方法)
来自于文献2
宏定义
staticNSString*kLocalCookieName =@"MyProjectCookie";
staticNSString*kLocalUserData =@"MyProjectLocalUser";
staticNSString*kServerSessionCookie =@"JSESSIONID";
保存登陆COOKIE
这个操作是保存NSHTTPCookieStorage对象里的所有cookie,当然也可以使用cookieForUrl来获取cookie进行保存,方便后续的使用。此操作是在登录的时候进行。
更新Cookie
** 这个方法就是更新cookie。在程序每次启动的时候调用一下。用来确保每次的cookie是最新的。**
退出登陆 清除cookie
** 这个方法就是移除你保存的cookie了。因为你在退出账户的时候需要重新获取新的cookie,所以,你需要在退出账户的时候清空你上次保存的cookie. **。
cookie设置遇到的坑:
1.使用字典类型实例化NSHTTPCookie时,字典的key需要使用NSHTTPCookie.h里指定的名称。
举个例子需要设定一下cookie来请求url时
qingpinwo_user_test_1=c085a7f4285fc11239330c0dafd166ab0976c877b9702f93;
domain=su.qingpinwo.com;
path=job-student-web;
只能
//设置HTTP Cookie值
NSMutableDictionary *cookieProperties = [NSMutableDictionary new];
[cookieProperties setObject:@"qingpinwo_user_test_1" forKey:NSHTTPCookieName];
[cookieProperties setObject:@"c085a7f4285fc11239330c0dafd166ab0976c877b9702f93" forKey:NSHTTPCookieValue];
NSHTTPCookie *cookietemp = [NSHTTPCookie cookieWithProperties:cookieProperties];
而不能:
[cookieProperties setObject:@"c085a7f4285fc11239330c0dafd166ab0976c877b9702f93" forKey:@"qingpinwo_user_test_1"];