Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maintain Session information by handling cookies in iOS

I am new in iOS development. I am using NSURLSession to manage session information. below is the sample code I use to call any server API,

NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request
                                                             completionHandler:^(NSData *data,
                                                                                 NSURLResponse *response,
                                                                                 NSError *error)
{ }];

My flow of application is, If not logged in -> Login (call login api) Else Go to home screen and call other APIs.

My problem here is, once the application is removed from memory, the session information is not maintained and I have to call Login again. My requirement is something like Facebook, where user has to login only once and his session is maintained throughout next app launches.

EDIT: I think I have to handle this by getting and setting cookies to these requests. I searched on this but didn't found any proper sample. can anyone please help me with some good sample regarding my issue.

Thanks!

like image 933
Azhar Bandri Avatar asked Jul 08 '14 07:07

Azhar Bandri


1 Answers

I finally found the solution by mixing answers from multiple posts. If anyone knows any better approach or any corrections in following method, please do post it.

In my call to login API, I do the following,

NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request
                                                             completionHandler:^(NSData *data,
                                                                                 NSURLResponse *response,
                                                                                 NSError *error)
{
    NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;

    NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:[httpResp allHeaderFields] forURL:[response URL]];
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookies forURL:[response URL] mainDocumentURL:nil];
    for (NSHTTPCookie *cookie in cookies) {
        NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
        [cookieProperties setObject:cookie.name forKey:NSHTTPCookieName];
        [cookieProperties setObject:cookie.value forKey:NSHTTPCookieValue];
        [cookieProperties setObject:cookie.domain forKey:NSHTTPCookieDomain];
        [cookieProperties setObject:cookie.path forKey:NSHTTPCookiePath];
        [cookieProperties setObject:[NSNumber numberWithInt:cookie.version] forKey:NSHTTPCookieVersion];

        [cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:31536000] forKey:NSHTTPCookieExpires];

        NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
        NSLog(@"name:%@ value:%@", cookie.name, cookie.value);
    }
}];

The most important part here is "dateByAddingTimeInterval:31536000". this sets the cookie expiration time in seconds. if this is not set, the session is maintained for only one time use.

Finally on logout, I clear all the cookies.

NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *each in cookieStorage.cookies) {
    [cookieStorage deleteCookie:each];
}
like image 72
Azhar Bandri Avatar answered Nov 17 '22 10:11

Azhar Bandri