首先,我在登陸成功的回調函數中就記錄下當時的cookie,並保存在本地中,全局調用。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,並存放在請求頭中便可url
- [self.manager.requestSerializer setValue:[[NSUserDefaults standardUserDefaults]objectForKey:@"userDefaultsCookie"]forHTTPHeaderField:@"Cookie"];