开发者

Cookies in UIWebView

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?


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.


Try changing the cookie acceptance policy instead:

[NSHTTPCookieStorage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyNever];


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜