0

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Zarochintsev
  • 221
  • 2
  • 10

1 Answers1

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