iphone NSHTTPCookieStorage 在应用程序重新打开时可用吗?
我正在这里开发我的应用程序 - 基本上就是这样。我有一个登录框,用户在其中登录,然后它在返回时保存 cookie 数据,如下所示:
NSArray * all = [NSHTTPCookie cookiesWithResponseHeaderFields:[resp allHeaderFields] forURL:[NSURL URLWithString:@"http://myurl]];
NSHTTPCookieStorage *sharedHTTPCookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
[sharedHTTPCookieStorage setCookies:all forURL:[NSURL URLWithString:@"http://myurl"] mainDocumentURL:nil];
在它保护 cookie 后,我将其带到主视图 - 我的问题是 - 如果用户关闭 prgoram,手机会重新启动并且等等 - cookie 本身存储在手机本地吗?我试图在 didFinishLaunchingWithOptions 上再次访问该 cookie。我现在有以下代码..
NSHTTPCookieStorage *sharedHTTPCookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookies = [sharedHTTPCookieStorage cookiesForURL:[NSURL URLWithString:@"http://iphone.wazgood.com"]];
NSLog(@"count: %i", [cookies count]);
每次 - cookie 数据上都是空的 - 关于每次用户关闭程序时是否清除 cookie 有什么想法 - 或者是 bc 我在 iPhone 模拟器上测试?
I am working on my app here- and it pretty much comes to this. I have a login box where a users log's in and then it saves the cookie data on return like so:
NSArray * all = [NSHTTPCookie cookiesWithResponseHeaderFields:[resp allHeaderFields] forURL:[NSURL URLWithString:@"http://myurl]];
NSHTTPCookieStorage *sharedHTTPCookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
[sharedHTTPCookieStorage setCookies:all forURL:[NSURL URLWithString:@"http://myurl"] mainDocumentURL:nil];
After it safes that cookie i take it to the home view - My problem is - if the users closes the prgoram, the phone restarts and so forth - are the cookies stored locally on the phone its sefl? I am trying to access that cookie again on the didFinishLaunchingWithOptions. I have the following code now..
NSHTTPCookieStorage *sharedHTTPCookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookies = [sharedHTTPCookieStorage cookiesForURL:[NSURL URLWithString:@"http://iphone.wazgood.com"]];
NSLog(@"count: %i", [cookies count]);
Every time - it comes up empty on the cookie data - any ideas on if the cookies are cleared every time the user clsoes the program out - or is it bc im testing on the iPhone emulator?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您或其他人仍然遇到此问题,可能是 cookie 设置为在会话结束时(应用程序关闭时)过期。您可以通过查看
NSHTTPCookie
的sessionOnly
属性(获取方法为-(BOOL)isSessionOnly
)来检查此行为。In case you or anyone else is still having this problem, it could be that the cookies are set to expire when the session ends (when the app closes). You can check for this behavior by looking at the
sessionOnly
property of yourNSHTTPCookie
s (the getter method is-(BOOL)isSessionOnly
).