如何将 cookie 放入 iPhone 应用程序的共享存储中?
我正在使用 Monotouch 开发 iPhone 应用程序。我正在使用 Web 请求来调用返回 cookie 的服务。我的主要目标是能够根据需要在各种视图控制器中使用 cookie 来处理 Web 请求。我假设使用 cookie 的共享存储是正确的方法。
我尝试过这一点,发现了以下障碍:
- HttpWebResponse 有一个由 System.Net.Cookie 对象组成的 cookie 集合。
- NSSharedStorage 对象仅接受 NSHttpCookie 对象。
我一直无法找到将 System.Net.Cookie 对象转换为 NSHttpCookie 对象的方法。这主要是因为 NSHttpCookie 仅具有只读属性,并且我不确定如何使用 C# 中的构造函数/静态函数创建一个属性。
所以我真的有两个问题:
- 将 cookie 插入共享存储是实现我的目标的最佳方式吗?
- 如果是这样,我该怎么做?
更新:
只是为了进一步澄清一点。答案此处 正在做与我想做的相反的事情。
I am using Monotouch to develop an iPhone App. I am using a web request to call a service that returns a cookie. My main aim is to be able to use the cookie as needed for web requests in various view controllers. I am assuming using the Shared Storage for the cookie is the way to go.
I have had a go at this and found the following hurdles:
- The HttpWebResponse has a cookie collection made up of System.Net.Cookie objects.
- The NSSharedStorage object only takes NSHttpCookie objects.
I haven't been able to figure out a way to turn a System.Net.Cookie object into an NSHttpCookie object. This is mainly because NSHttpCookie has only readonly properties and I am not sure how to create one using the constructors/static functions in C#.
So I really have two questions:
- Is inserting the cookie into shared storage the best way to achieve my aim?
- If so, how do I do this?
UPDATE:
Just to clarify a little further. The answer here is doing the opposite of what I want to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为有 NSSharedStorage 这样的东西。是 NSHTTPCookieStorage 你的意思是什么?如果是这样,这对你不起作用。根据文档:“iOS 中的应用程序之间不会共享 Cookie”。也没有一种快速的方法可以做你想做的事。您必须执行更新中提供的链接中建议的操作。即创建一个执行转换的扩展方法(或某种实用方法)。
您可能需要在应用程序中管理自己的 System.Net.Cookie 集合。
I don't think there's such thing as a NSSharedStorage. Is NSHTTPCookieStorage what you mean? If so, this wouldn't work for you. As per the documentation: "Cookies are not shared among applications in iOS". There also isn't a quick way to do what you want. You'll have to do something like what is suggested in the link you provided in your update. i.e. create an extension method (or some kind of utility method) that does the conversion.
You will likely have to manage your own collection of System.Net.Cookies in your app.