How to save cookies, and then stick them in a query? I understand that this is a stupid question, but I can not find a solution not.
Asked
Active
Viewed 1,786 times
0
-
use [`NSUserDefaults`](http://stackoverflow.com/a/3074489/1066828) – Fahim Parkar Jun 26 '14 at 21:03
1 Answers
6
Write sometime after the first request to the server (authorization - when cookies do not need to pass)
NSData *cookieData = [NSKeyedArchiver archivedDataWithRootObject:[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]];
[[NSUserDefaults standardUserDefaults] setObject:cookieData forKey:@"ApplicationCookie"];
[[NSUserDefaults standardUserDefaults] synchronize];
Then, when the application starts every time pull cookies from NSUserDefaults, if they are there then:
NSData *cookieData = [[NSUserDefaults standardUserDefaults] objectForKey:@"ApplicationCookie"];
if ([cookieData length] > 0) {
NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData:cookieData];
for (NSHTTPCookie *cookie in cookies) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}
}

Zarochintsev
- 221
- 2
- 10