Can AFNetworking 2 store the cookies

不羁岁月 提交于 2019-12-07 15:44:56

问题


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.

Now I use this code:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

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

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

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.

How can I solve this?

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

I have checked cookies.

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>

But if I rerun app the cookie are changed.

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?


回答1:


From Persisting Cookies in an iOS Application:

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 does not do anything with cookies under the hood. It does use NSURLConnection, and NSURLConnection uses NSHTTPCookieStorage.

According to your log:

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

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

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

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



来源:https://stackoverflow.com/questions/22078151/can-afnetworking-2-store-the-cookies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!