Cookies in UIWebView

南楼画角 提交于 2019-11-29 18:32:00

问题


I have a UIWebView, and I don't want it to store an cookies, so just before the webview is loaded I do:

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

Checking the count of cookies is 0 so they are all deleted. But when I go to stackoverflow it still recognizes my Google Account and logs me in. How does this happen? I though it worked with cookies?


回答1:


I had to deal with the exact same issue and I found 2 ways to deal with that problem. I first noticed that cookies get (sometimes) set at weird times (Strange behaviour especially with ios 4.0).

  • Instant deletion of cookies after the user was on a webview often didn't get me the expected results.

I then integrated a persistant, manual flag that was set True on a "logout" (aka clear all cookies / delete other user data) action. upon the next login (aka user-login-based action) I cleared the cookies again (the same way you did it in your code-post).

Later I found out, that listening to NSHTTPCookieManagerCookiesChangedNotification and then deleting cookies worked really well too.

hope I could help.




回答2:


Try changing the cookie acceptance policy instead:

[NSHTTPCookieStorage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyNever];



回答3:


Use following and it will work..

   NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
   NSHTTPCookie *cookie;
   for(cookie in [storage cookies])
    {
      NSLog(@"cookie to be deleted:%@", cookie);
      [storage deleteCookie:cookie];
    }
   [[NSUserDefaults standardUserDefaults] synchronize];

here don't miss last line [[NSUserDefaults standardUserDefaults] synchronize]; otherwise you will remain puzzle.



来源:https://stackoverflow.com/questions/3646879/cookies-in-uiwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!