objective-c 如何多次清除 NSHTTPCookieStorage 中的 cookie?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1852515/
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
How to clear cookies from NSHTTPCookieStorage more then once?
提问by Chris
My desktop app connects to a web application that's hosted on Google App engine. Once it authenticates it gets an authtoken cookie that it passes along for all future requests. That all works.
我的桌面应用程序连接到托管在 Google App 引擎上的网络应用程序。一旦它通过身份验证,它就会获得一个 authtoken cookie,它将为所有未来的请求传递。这一切都有效。
But now I want to add "Sign out". I've tried to implement Sign Out like this:
但现在我想添加“退出”。我试过像这样实现注销:
- (void)signOut {
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *each in [[[cookieStorage cookiesForURL:[NSURL URLWithString:self.serviceRootURLString]] copy] autorelease]) {
[cookieStorage deleteCookie:each];
}
[self clearCredentialStorage];
}
The problem is that it only seems to work the first time. For instance I can open my app. Sign in. Make some requests. Sign out. Then next time I make a request I'm asked to authenticate again. Good!
问题是它似乎只在第一次起作用。例如,我可以打开我的应用程序。登录。提出一些要求。登出。然后下次我提出请求时,我会被要求再次进行身份验证。好的!
But after I authenticate the second time the problem happens. The authentication works. I get the authtoken cookie. I can make requests. But then when I try to log out for the second time (without restarting my app) the authtoken cookie doesn't seem to get deleted. It does seem deleted from my apps perspective... I ask NSHTTPCookieStorage for the cookies that it has my URL and it returns none. But if I try to make another request (that should require the authtoken cookie) the request just works, I don't get a 401 response and I'm never asked to authenticate again.
但是在我第二次进行身份验证后,问题发生了。身份验证有效。我得到了 authtoken cookie。我可以提出要求。但是当我第二次尝试注销时(不重新启动我的应用程序),authtoken cookie 似乎没有被删除。从我的应用程序的角度来看,它似乎被删除了......我向 NSHTTPCookieStorage 询问它有我的 URL 的 cookie,但它没有返回。但是,如果我尝试发出另一个请求(应该需要 authtoken cookie),该请求就可以正常工作,我不会收到 401 响应,也不会再被要求进行身份验证。
So if I'm understanding things correctly it seems that the cookies are deleted from my perspective, but they are not deleted from the underlying URL loading frameworks perspective.
因此,如果我理解正确的话,从我的角度来看,cookie 似乎已被删除,但从底层 URL 加载框架的角度来看,它们并未被删除。
Of possible interest, maybe the problem is related to: http://www.macworld.com/article/143343/2009/10/safaricookieproblems.html
可能感兴趣,也许问题与:http: //www.macworld.com/article/143343/2009/10/safaricookieproblems.html
Does anyone know how to consistently implement "log out" functionality in an app that interacts with a web service?
有谁知道如何在与 Web 服务交互的应用程序中始终如一地实现“注销”功能?
Thanks, Jesse
谢谢,杰西
采纳答案by benzado
Theory: there are other relevant cookies that are not under your service's URL, because some requests result in a redirect which may get or set cookies on your system.
理论:还有其他相关 cookie 不在您服务的 URL 下,因为某些请求会导致重定向,这可能会在您的系统上获取或设置 cookie。
Also: I recommend using tcpdump for debugging. You'll be able to see exactly what is going over the network, and know exactly what cookies are being sent or received.
另外:我建议使用 tcpdump 进行调试。您将能够准确地看到网络上正在发生的事情,并确切地知道正在发送或接收哪些 cookie。
回答by Chris
+(void)clearAllCookies {
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *each in cookieStorage.cookies) {
[cookieStorage deleteCookie:each];
}
}
I know this is an old question, but here is the answer. We are currently using this with our app to clear out ALL cookies for UIWebView(this only has access to cookies in your app, not shared cross apps in any way).
我知道这是一个老问题,但这是答案。我们目前在我们的应用程序中使用它来清除所有 cookie UIWebView(这只能访问您应用程序中的 cookie,不能以任何方式共享跨应用程序)。
If you want to clear only specific once you can always check the properties of each cookie object deciding if you want to delete it or not. for example cookie.name isEqualToString@"somename"
如果您只想清除特定的一次,您可以随时检查每个 cookie 对象的属性,决定是否要删除它。例如 cookie.name isEqualToString@"somename"
Hopefully this helps someone out there.
希望这可以帮助那里的人。
Edit: as of some iOS version there is an easier way now, there is a new method to clear out cookies by date.
编辑:从某些 iOS 版本开始,现在有一种更简单的方法,有一种按日期清除 cookie 的新方法。
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
[cookieStorage removeCookiesSinceDate:[NSDate dateWithTimeIntervalSince1970:0]];
SWIFT 4.X version: (a lot simpler, one liner)
SWIFT 4.X 版本:(简单很多,一个班轮)
HTTPCookieStorage.shared.cookies?.forEach(HTTPCookieStorage.shared.deleteCookie)

