项目用到了 把用户信息保存在 cookie 中,类似浏览器记录用户信息一样,然后请求的接口从cookie中获取 当前用户信息,这就要求我们每次登陆后把 cookie 保存起来,还有退出程序 清空cookie的 问题,由于项目中用到了 AFN,所以导致了一些难以发现的问题。
首先,用户第一次 注册APP,请求注册接口 后台会把cookie 返回回来,我们需要把cookie保存起来,代码如下:
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
NSString *cookieString = [response.allHeaderFields valueForKey:@"Set-Cookie"];
if (cookieString) {
NSArray *cookieStrings = [cookieString componentsSeparatedByString:@","];
for(NSString* temp in cookieStrings)
{
NSArray *tempArr = [temp componentsSeparatedByString:@";"];
[finalCookie appendString:[NSString stringWithFormat:@"%@",tempArr[0]]];
}
[[XSTUserDefault sharedXSTUserDefault]loadUserInfoFormSanbox];
[XSTUserDefault sharedXSTUserDefault].cookie = finalCookie;
[[XSTUserDefault sharedXSTUserDefault]saveUserInfoToSanbox];
[[NSUserDefaults standardUserDefaults] setObject:cookieString forKey:@"UserCookies"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
// 单例 项目中共享
NSHTTPCookieStorage *myCookie = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSMutableDictionary *mutdict = [NSMutableDictionary dictionary];
[mutdict setObject:@"SESSION" forKey:NSHTTPCookieName];
[mutdict setObject:cookieString forKey:NSHTTPCookieValue];
[mutdict setObject:@"api.xiaoshentu.com" forKey:NSHTTPCookieDomain];
[mutdict setObject:@"api.xiaoshentu.com" forKey:NSHTTPCookieOriginURL];
[mutdict setObject:@"/" forKey:NSHTTPCookiePath];
[mutdict setObject:@"0" forKey:NSHTTPCookieVersion];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:mutdict];
[myCookie setCookie:cookie];
NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject: [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]];
//存储归档后的cookie 由于网页中用的数据是后台从 cookie取得所以需要保存起来,每次启动APP 在加载下 cookie
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject: cookiesData forKey: @"myCookie"];
每次请求接口需要把 cookie传给后台,由于用到的是AFN 当cookie 为空时 需要传给空字符串给后台,不然换个用户登录后台不会返回当前用户cookie,代码如下
[[XSTUserDefault sharedXSTUserDefault]loadUserInfoFormSanbox];
NSString *cookieString = [XSTUserDefault sharedXSTUserDefault].cookie;
NSLog(@"%@",cookieString);
if(cookieString){
[mgr.requestSerializer setValue: cookieString forHTTPHeaderField:@"Cookie"];
}else{
[mgr.requestSerializer setValue:@"" forHTTPHeaderField:@"Cookie"];
}
由于APP 中加载网页需要用户信息,后台是从cookie取得,由于网页是默认加载cookie的 所以每次启动APP 需要加载下当前的 保存的cookie,代码如下:
//合适的时机加载持久化后Cookie 一般都是app刚刚启动的时候
- (void)loadSavedCookies{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//对取出的cookie进行反归档处理
NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData:[userDefaults objectForKey:@"myCookie"]];
if (cookies) {
NSLog(@"有cookie");
//设置cookie
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (id cookie in cookies) {
[cookieStorage setCookie:(NSHTTPCookie *)cookie];
}
}else{
if ([[NSUserDefaults standardUserDefaults]objectForKey:@"UserCookies"]) {
NSHTTPCookieStorage *myCookie = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSMutableDictionary *mutdict = [NSMutableDictionary dictionary];
[mutdict setObject:@"SESSION" forKey:NSHTTPCookieName];
[mutdict setObject:[[NSUserDefaults standardUserDefaults]objectForKey:@"UserCookies"] forKey:NSHTTPCookieValue];
// 后面几个参数 一定要设置,否则发现 cookie 是空的
[mutdict setObject:@"api.xiaoshentu.com" forKey:NSHTTPCookieDomain];
[mutdict setObject:@"api.xiaoshentu.com" forKey:NSHTTPCookieOriginURL];
[mutdict setObject:@"/" forKey:NSHTTPCookiePath];
[mutdict setObject:@"0" forKey:NSHTTPCookieVersion];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:mutdict];
[myCookie setCookie:cookie];
}
NSLog(@"无cookie");
}
//打印cookie,检测是否成功设置了cookie
NSArray *cookiesA = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
for (NSHTTPCookie *cookie in cookiesA) {
NSLog(@"setCookie: %@", cookie);
}
}
加载网页请求是需要开启加载cookie,这样网页就能获取用户信息了。
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.url]];
[request setHTTPShouldHandleCookies:YES];
[self.h5WebView loadRequest:request];
有问题 请加微信 743278081