Maintain Session information by handling cookies i

2020-05-27 11:35发布

问题:

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!

回答1:

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];
}