ios 如何使用 AFNetworking 管理会话?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10984374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:42:55  来源:igfitidea点击:

How to manage sessions with AFNetworking?

iossessioncookiesafnetworking

提问by Bart Jacobs

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.

正如标题所暗示的,我在一个应用程序与服务器通信的 iOS 项目中使用 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?

我想知道 AFNetworking 是否会在每个后续请求中自动发送会话 ID,还是我应该以某种方式自己处理?

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.

供您参考,就请求的身份验证方式而言,我无法控制后端。我只是在构建一个与服务器对话的客户端。

回答by Michael Boselowitz

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.

是的,一旦您登录,您的会话 ID 就应该自动发送,只要 cookie 在发送下一个请求之前没有过期(重要的细节需要确保)。NSURLConnectionAFNetworking 使用的 ,会为您处理这些细节。

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

在后端 AFNetworking 正在使用NSURLConnection它反过来自动更新NSHTTPCookieStorage以存储会话。您可以通过弄乱 cookie 存储来操作或删除您认为合适的 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 :

就像如果您想在服务中显示为未登录,您可以删除与该域关联的会话 cookie。如果您已经登录并尝试再次登录,我使用过的某些服务会出错。此外,无法检查登录状态。快速修复,从 URL 获取 cookie 并删除它们:

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

From the developer himself

来自开发者本人

回答by TeaCupApp

This depends on the specification given by the service you are interacting with. I have worked with similar services and they have explicitly stated in their documentation that, In order to maintain the session valid I must poll the service at every few seconds by sending int "1".

这取决于您与之交互的服务给出的规范。我有类似的服务工作,他们在文件中已明确指出,为了保持该会话有效我每隔几秒钟发送INT“1”必须轮询服务。

However, If possible could please post the service name or any reference which we can read. If it is any company's private API then they must have described the use of the session id which they are returning.

但是,如果可能,请发布服务名称或我们可以阅读的任何参考资料。如果它是任何公司的私有 API,那么他们必须描述了他们返回的会话 ID 的使用。



Underlying technologies will take care of it, However if you want to persist those cookies then this answer for other question.

底层技术会处理它,但是,如果您想保留这些 cookie,那么这个答案可以用于其他问题。

Persisting Cookies In An iOS Application?

在 iOS 应用程序中持久化 Cookie?