NSHTTPCookieStorage的共享实例不保留cookie [英] Shared instance of NSHTTPCookieStorage does not persist cookies

查看:157
本文介绍了NSHTTPCookieStorage的共享实例不保留cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,其中服务器向我发送一个cookie来识别用户.

I'm developing an application where the server hands me a cookie to identify the user.

我的后续请求需要具有该cookie,才能具有用户期望的响应. 我不明白的是,NSHTTPCookieStorage共享实例丢失Cookie的方式和时间.

My successive requests need to have that cookie to have the response that the user expects. What I can't understand is how and when the shared instance of NSHTTPCookieStorage loses its cookies.

我使用的第一个解决方案是在应用程序终止时将服务器中的cookie存档并保存到用户默认设置,然后在应用程序启动时从服务器中清除现有的cookie并重置我存储的cookie. 在开发过程中,我没有遇到问题,因为调试会话非常短,通常不需要将应用置于后台.

The first solution I used is to archive and save the cookies from my server to user defaults on application terminations then clear existing ones from my server on application launch and reset the ones I stored. Through the developing process I didn't encounter problems as the sessions for debugging are very short and didn't usually need to put the app in background.

在Beta测试期间,麻烦开始了. 我的技巧是不仅在应用程序终止时保存cookie,而且在API调用使我退回这些cookie之后也保存它们.并且不仅要在应用启动时加载已保存的Cookie,还要在应用返回前台时加载该Cookie.

During beta testing, troubles started. The hack I came with is to save the cookies not only on application termination but also after the API calls that hand me back these cookies. And to load the saved cookies not only on app launch but also when the app returns to foreground.

NSHTTPCookieStorage共享实例如何摆脱这些cookie,以及如何处理它的最佳实践是什么,因为它是我的应用程序的重要组成部分,如果没有经验丰富的开发人员的支持,我将不相信这种黑客入侵的解决方案.

How come the NSHTTPCookieStorage share instance gets rid of these cookies and what's the best practice to deal with it as it's a vital part of my app and I can't trust such a hacked solution if not backed up by more experienced developers.

预先感谢您的回答

这是保存/读取/清除cookie的方法

Here are the methods to save/read/clear the cookies

-(void)saveStoredCookies
{
    NSURL *httpUrl = @"http://myServer.com";
    NSURL *httpsUrl = @"https://myServer.com";

    NSArray *httpCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:httpUrl];
    NSData *httpCookiesData = [NSKeyedArchiver archivedDataWithRootObject:httpCookies];
    [[NSUserDefaults standardUserDefaults] setObject:httpCookiesData forKey:@"savedHttpCookies"];

    NSArray *httpsCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:httpsUrl];
    NSData *httpsCookiesData = [NSKeyedArchiver archivedDataWithRootObject:httpsCookies];
    [[NSUserDefaults standardUserDefaults] setObject:httpsCookiesData forKey:@"savedHttpsCookies"];

    [[NSUserDefaults standardUserDefaults] synchronize];
}

-(void)readStoredCookies
{
    //clear, read and install stored cookies
    NSURL *httpUrl = @"http://myServer.com";
    NSURL *httpsUrl = @"https://myServer.com";

    NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:httpUrl];
    for (NSHTTPCookie *cookie in cookies) {
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
    }
    cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:httpsUrl];
    for (NSHTTPCookie *cookie in cookies) {
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
    }

    NSData *httpCookiesData = [[NSUserDefaults standardUserDefaults] objectForKey:@"savedHttpCookies"];
    if([httpCookiesData length]) {
        NSArray *savedCookies = [NSKeyedUnarchiver unarchiveObjectWithData:httpCookiesData];
        for (NSHTTPCookie *cookie in savedCookies) {
            [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
        }       
    }
    NSData *httpsCookiesData = [[NSUserDefaults standardUserDefaults] objectForKey:@"savedHttpsCookies"];
    if([httpsCookiesData length]) {
        NSArray *savedCookies = [NSKeyedUnarchiver unarchiveObjectWithData:httpsCookiesData];
        for (NSHTTPCookie *cookie in savedCookies) {
            [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
        }       
    }
}

-(void)clearStoredCookies
{
    NSURL *httpUrl = @"http://myServer.com";
    NSURL *httpsUrl = @"https://myServer.com";
    NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:httpUrl];
    for (NSHTTPCookie *cookie in cookies) {
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
    }
    cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:httpsUrl];
    for (NSHTTPCookie *cookie in cookies) {
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
    }
}

推荐答案

已解决!

经过一些简单的调试后,我发现问题出在我在cookiesForURL:中使用的url. 我只是开始使用cookies属性,现在它可以正常工作.

After some simple debugging I found out that the problem was with the url I was using in cookiesForURL:. I simply started using cookies property and now it works fine.

这篇关于NSHTTPCookieStorage的共享实例不保留cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆