如何管理与AFNetworking的会话? [英] How to manage sessions with AFNetworking?

查看:130
本文介绍了如何管理与AFNetworking的会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所暗示的,我在一个iOS项目中使用AFNetworking,其中应用程序与服务器通信。当用户登录时,服务器通过发送成功标志进行响应,并且响应头包含会话ID。

As the title implies, I am using AFNetworking in an iOS project in which the application talks to a server. When the user signs in, the server responds by sending back a success flag and the response headers contain the session ID.

我想知道AFNetworking是否自动发送会话ID每个后续请求,或者我应该以某种方式照顾自己?

I am wondering if AFNetworking automatically sends the session ID with every subsequent request or should I take care of this myself in some way?

为了您的信息,我不能控制后端如何请求的身份验证。

For your information, I have no control over the back-end in terms of how requests are authenticated. I am only building a client that talks to the server.

推荐答案

是的,您的会话ID应该在您登录后自动发送in,只要cookie不会在下一个请求发送之前过期(重要的细节是肯定的)。 AFNetworking使用的NSURLConnection 会为您处理此操作的详细信息。

Yes, your session ID should be sent automatically once you are logged in, as long as the cookie does not expire before the next request is sent (important detail to be sure of). NSURLConnection, which AFNetworking uses, takes care of the details for this for you.

在后端AFNetworking正在使用 NSURLConnection ,它会自动更新 NSHTTPCookieStorage 来存储会话。

On the backend AFNetworking is using NSURLConnection which in turn automatically updates NSHTTPCookieStorage to store the session. You can manipulate or delete the cookies as you see fit by messing with the cookie storage.

如果您想要在服务中显示为未登录,您可以只是删除与该域相关联的会话cookie。如果您已经登录并尝试重新登录,我使用的一些服务将会错误。此外,没有办法检查登录状态。快速修复,从URL获取cookie并删除它们:

Like if you wanted to appear to the service as not logged in, you could just delete the session cookie associated to that domain. Some services I have worked with will error if you are already logged in and attempt to login again. Additionally there was no way to check login status. Quick fix, get the cookies from URL and delete them :

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

从开发者自己

这篇关于如何管理与AFNetworking的会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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