NSURLConnection会自动保留从服务器发送的cookie吗? [英] Does NSURLConnection automatically persist cookies sent from server?

查看:110
本文介绍了NSURLConnection会自动保留从服务器发送的cookie吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从ios登录龙卷风后端并发回一个secure_cookie,我注意到只要我验证了我设置的secure_cookie,我也可以请求其他信息。 NSURLConnection持续cookie多久或者关闭应用后cookie会被删除?

I logged into my tornado backend from ios and sent back a secure_cookie and i noticed that i could also request other information as long as i validated the secure_cookie that i set. How long does NSURLConnection persist the cookie or will the cookie be deleted once they close the app?

这在Apple文档中提到:

This is mentioned in the Apple docs:


URL加载系统自动发送适用于NSURLRequest的任何存储的cookie
。除非请求指定不发送
发送cookie。

The URL loading system automatically sends any stored cookies appropriate for an NSURLRequest. unless the request specifies not to send cookies.


推荐答案

一些方面您的问题。

首先,NSURLConnection默认情况下会根据设备的设置管理Cookie。它会将cookie保存到商店并根据需要发送(当域与现有cookie匹配时)。这意味着如果您从带有保存在cookie存储中的cookie的URL发出请求,它将被发送。这解释了您提到的行为。通常,使用默认设置时,cookie将持续相当长的一段时间。

To start with, NSURLConnection will, by default, manage cookies based on the settings for the device. It will save the cookies to the store and send them as appropriate (when the domain matches an existing cookie). This means if you make a request from a URL with a cookie saved in the cookie store, it will be sent. This explains the behavior you mentioned. Generally, with the default settings, the cookie will persist for quite a while.

但是,这个初始声明可能对您的需求没有帮助。我假设您可能希望控制此 secure_cookie 字段的过期(或保持永久),以便您的应用程序将来不必进一步进行身份验证。

This initial statement, however, maybe is not helpful for your needs. I am assuming you may want to have control over the expiration of (or keep around "forever") this secure_cookie field so your app does not have to authenticate further in the future.

在这种情况下, NSHTTPCookieStorage 是值得一看的地方。这个类允许你同时检索:

In this case, NSHTTPCookieStorage is the place to look. This class will allow you to both retrieve:


[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@ http://example.com ]]

并设置:

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie] (加上设置cookie字典对象)

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie] (plus setting up the cookie dictionary object)

根据经验和在线其他人的反馈,您应该注意到cookie存储并不总是100%可靠。如果您希望将特定cookie和值发送到服务器,则应将该值与您的应用程序(例如prefs或Core Data)一起存储,并使用相应的 NSHTTPCookieStorage重置cookie

Based on experience and the feedback of others online, you should note that the cookie storage is not always 100% reliable. If you would like to be sending a specific cookie and value to the server, you should store that value with your app (prefs or Core Data, for example), and reset the cookie with the appropriate NSHTTPCookieStorage at each startup.

这篇关于NSURLConnection会自动保留从服务器发送的cookie吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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