我是iOS开发的新手.我正在使用NSURLSession来管理会话信息.下面是我用来调用任何服务器API的示例代码,
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request
                                                             completionHandler:^(NSData *data,NSURLResponse *response,NSError *error)
{ }]; 
 我的申请流程是,如果没有登录 – >登录(呼叫登录api)Else转到主屏幕并调用其他API.
我的问题是,一旦从内存中删除应用程序,会话信息就不会被维护,我不得不再次调用Login.我的要求就像Facebook一样,用户只需登录一次,并且在下次应用程序启动时保持会话.
编辑:
我想我必须通过获取和设置cookie来处理这些请求.我搜索了这个,但没有找到任何适当的样本.任何人都可以帮我提一些关于我的问题的好样本.
谢谢!
解决方法
 我终于通过混合多个帖子的答案找到了解决方案.如果有人知道任何更好的方法或以下方法的任何更正,请发布它. 
  
 
        在我对登录API的调用中,我执行以下操作,
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request
                                                             completionHandler:^(NSData *data,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到期时间.如果未设置,则会话仅保留一次.
最后退出时,我清除了所有的cookie.
NShttpcookiestorage *cookieStorage = [NShttpcookiestorage sharedhttpcookiestorage];
for (NSHTTPCookie *each in cookieStorage.cookies) {
    [cookieStorage deleteCookie:each];
}