- 1 由于項(xiàng)目中后臺(tái)接口需要我每次發(fā)送請(qǐng)求的時(shí)候 發(fā)送cookie荡澎,之前沒(méi)這樣做過(guò),所以遇到了很多問(wèn)題晤锹,這樣做的好處就是用戶(hù)退出后臺(tái)下次在進(jìn)入APP 用過(guò)判斷cookie 就能知道是不是 還是上次登錄的用戶(hù)摩幔,如果是的話(huà)就不用登錄了,就能獲取到用戶(hù)信息抖甘。之前做的項(xiàng)目都是 把userI'd 存到本地 請(qǐng)求的時(shí)候把userid 帶上 其實(shí)原理差不多热鞍。
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:URL]];
NSDictionary *cookieHeaders = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
NSSting *cookie = [cookieHeaders objectForKey:@"Cookie"];
if([NSUserDefault sharDefatult] objectForKey:@"Cookie"] !=nil){
mgr.requestSerializer setValue:[NSUserDefault sharDefatult] objectForKey:@"Cookie" forHTTPHeaderField:@"Cookie"];
}else{
[NSUserDefatult shaerDefault] setObject:cookie forKey:@"Cookie"];
mgr.requestSerializer setValue:[NSUserDefault sharDefatult] objectForKey:@"Cookie" forHTTPHeaderField:@"Cookie"];
}
就是每次發(fā)請(qǐng)求之前給 請(qǐng)求頭 帶上cookie 后臺(tái)去判斷 是不是一個(gè)會(huì)話(huà)。