2

I am trying to save session on UIWebView as follows as suggested in this link :

- (NSURLRequest*)addCookies:(NSArray *)cookies forRequest:(NSURLRequest *)request
{
    NSMutableURLRequest *mutableRequest = [request mutableCopy];
    if ([cookies count] > 0)
    {
        NSHTTPCookie *cookie;
        NSString *cookieHeader = nil;
        for (cookie in cookies)
        {
            if (!cookieHeader)
            {
                cookieHeader = [NSString stringWithFormat: @"%@=%@",[cookie name],[cookie value]];
            }
            else
            {
                cookieHeader = [NSString stringWithFormat: @"%@; %@=%@",cookieHeader,[cookie name],[cookie value]];
            }
        }
        if (cookieHeader)
        {
            [mutableRequest setValue:cookieHeader forHTTPHeaderField:@"Cookie"];
        }

        return [mutableRequest copy];
    }

    return nil;
}  

And in viewDidLoad :

NSURLRequest *webRequest = [NSURLRequest requestWithURL:homeURL];
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];

NSMutableURLRequest *requestMut = [NSMutableURLRequest requestWithURL:webRequest.URL];
[requestMut setHTTPShouldHandleCookies:YES];
webRequest = [self addCookies:cookies forRequest:requestMut];
[_tgWebView loadRequest:webRequest];  

But the application is again coming on login screen i.e. session is not saving. This is a similar question unanswered I just found.
Update: I tried to set the expiry date of cookies as around ten years ahead, but still the issue isn't fixing :

+ (void)loadCookies
{
    NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData: [[NSUserDefaults standardUserDefaults] objectForKey: @"SAVED_COOKIES"]];
    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

    for (NSHTTPCookie *cookie in cookies)
    {
        NSMutableDictionary *newProperties = [[NSMutableDictionary alloc]initWithDictionary:cookie.properties];
        NSDate *date = [newProperties objectForKey:NSHTTPCookieExpires];
        if(date == nil)
            [newProperties setObject:[[NSDate date] dateByAddingTimeInterval:100*12*30*60*60] forKey:NSHTTPCookieExpires];
        else
            [newProperties setObject:[[NSDate date] dateByAddingTimeInterval:100*12*30*60*60] forKey:NSHTTPCookieExpires];
        NSHTTPCookie *newCookie = [NSHTTPCookie cookieWithProperties:newProperties];
        [cookieStorage setCookie: newCookie];
        NSLog(@"%@",newCookie);
    }
}
Community
  • 1
  • 1
Nitish
  • 13,002
  • 23
  • 121
  • 243
  • Did you test it on simulator? If the app is running, you hit run button again, it may not have the time to save the cookie. I made a simple loadRequest and didn't have any code about cookie, and I didn't have to log in again. You can try it on a device and other URLs, it might be a server problem. – gabbler Jan 17 '15 at 16:17

1 Answers1

12

That's just a possible implementation : First store the cookies

- (void)saveCookies {
   NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject: [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]];
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   [defaults setObject: cookiesData forKey: @"cookies"];
   [defaults synchronize];
}

Restore the cookies :

- (void)loadCookies {
   NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey: @"cookies"]];
   NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
   for (NSHTTPCookie *cookie in cookies) {
    [cookieStorage setCookie: cookie];
   }
}
oiledCode
  • 8,419
  • 6
  • 41
  • 59