通过处理iOS中的Cookie来维护会话信息 [英] Maintain Session information by handling cookies in iOS
问题描述
我是iOS开发的新手。我使用NSURLSession来管理会话信息。下面是我用来调用任何服务器API的示例代码。
NSURLSessionDataTask * task = [[NSURLSession sharedSession] dataTaskWithRequest:request
completionHandler:^(NSData * data,
NSURLResponse * response,
NSError * error)
{}];
我的申请流程是,如果没有登录 - >登录(call login api)主屏幕并调用其他API。
我的问题是,一旦应用程序从内存中删除,会话信息不会维护,我必须再次调用登录。我的要求是像Facebook,用户只需要登录一次,他的会话在下次应用程序启动时保持。
编辑:
我想我必须通过获取和设置Cookie到这些请求来处理此问题。我在这里搜索,但没有找到任何适当的样品。
谢谢!
我终于找到了解决方案通过混合多个帖子的答案。
在我的登录API调用中,我执行以下操作:
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);
}
}];
这里最重要的部分是dateByAddingTimeInterval:31536000。这将设置cookie的过期时间(以秒为单位)。
最后,在注销后,我清除所有的cookies。
NSHTTPCookieStorage * cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for(NSHTTPCookie * each in cookieStorage.cookies){
[cookieStorage deleteCookie:each];
}
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!
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];
}
这篇关于通过处理iOS中的Cookie来维护会话信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!