AFNetworking 2可以存储Cookie吗 [英] Can AFNetworking 2 store the cookies

查看:94
本文介绍了AFNetworking 2可以存储Cookie吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了AFHTTPClient,似乎它在框中存储了cookie。因此,当我登录后,然后重新加载我的应用程序时,这些cookie便自动进入了应用程序。也许我对此感到困惑。

I used AFHTTPClient and seems it stored cookies in the box. So when I was logged in. and then reload my app, the cookies were in application automatically. Maybe I confused about this. But seems it works in the way I described.

现在我使用以下代码:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    [manager POST:path parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {  
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];

但是当我重新启动应用程序时,当我尝试请求一些api方法时收到错误的令牌消息

But when I restart my application I get message with wrong token when I try request some of my api method.

所以我如何使用上面的代码存储cookie。我将这段代码用于登录,注销和其他api服务的所有请求。如果我不重新启动,它会很好运行,但是我重新运行,似乎cookie掉了。

So how can I store my cookies using the code above. I use this code for all my request for login, logout and other api services. If I don't restart it works good, but i rerun it seems cookies are went from me.

我该如何解决?

我找到了这个答案 link ,但我不确定如何使用它。

I found this answer link but I am not sure how to use it.

我已经检查过cookie。

I have checked cookies.

如果我不重新运行应用程序,则cookie相同:

If I don't rerun application the cookies are the same:

<NSHTTPCookie version:0 name:"...." value:"....." expiresDate:(null) created:2014-02-27 19:24:29 +0000 (4.15222e+08) sessionOnly:TRUE domain:"...." path:"/" isSecure:FALSE>

但是如果我重新运行应用程序,cookie就会更改。

But if I rerun app the cookie are changed.

哦,我知道,该应用程序未保存cookie,看来我需要编写此功能。您有很好的建议怎么做吗?

Oh I understood, the app does not save cookies, seems I need to write this functional. Do you have good suggestion how to do it?

推荐答案

来自将Cookie保留在iOS应用程序中


没有有效期的Cookie被视为仅会话并在重新启动应用程序时清除。您可以通过 NSHTTPCookie 中的 BOOL 属性检查仅会话情况。这是标准的cookie内容,而不是iOS特有的东西。

Cookies without an expiry date are considered 'session only' and will get cleared when you restart the app. You can check the 'session only' situation via a BOOL property in NSHTTPCookie. This is standard cookie stuff and not something specific to iOS.

AFNetworking并不对cookie进行任何处理。它确实使用NSURLConnection,而NSURLConnection使用NSHTTPCookieStorage。

AFNetworking does not do anything with cookies under the hood. It does use NSURLConnection, and NSURLConnection uses NSHTTPCookieStorage.

根据您的日志:

<NSHTTPCookie version:0 name:"...." value:"....." expiresDate:(null) created:2014-02-27 19:24:29 +0000 (4.15222e+08) sessionOnly:TRUE domain:"...." path:"/" isSecure:FALSE>

您只有会话cookie( sessionOnly:TRUE )。

you have session only cookies ("sessionOnly:TRUE").

您需要在服务器端添加一个到期日期,以便在会话结束时NSHTTPCookieStorage不会将其丢弃。

You need to add an expiration date on the server end so that NSHTTPCookieStorage doesn't throw them away when your session ends.

这与AFNetworking无关;这就是Cookie的工作方式。

This is not anything AFNetworking-specific; it's just how cookies work.

这篇关于AFNetworking 2可以存储Cookie吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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