首先,我在登录成功的回调函数中就记录下当时的cookie,并保存在本地中,全局调用。
//获取cookie
- NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage]cookiesForURL:[NSURL URLWithString:url]];
- for (NSHTTPCookie *tempCookie in cookies)
- {
- //打印cookies
- NSLog(@"getCookie:%@",tempCookie);
- }
- NSDictionary *Request = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
- NSUserDefaults *userCookies = [NSUserDefaults standardUserDefaults];
- [userCookies setObject:[Request objectForKey:@"Cookie"] forKey:@"userDefaultsCookie"];
- [userCookies synchronize];
再在你需要的页面中调用出你的cookie,并存放在请求头中即可
- [self.manager.requestSerializer setValue:[[NSUserDefaults standardUserDefaults]objectForKey:@"userDefaultsCookie"]forHTTPHeaderField:@"Cookie"];