删除 cookie UIWebView

发布于 2024-12-06 10:53:11 字数 307 浏览 1 评论 0原文

如何删除UIWebView中的cookie?此代码:

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

删除 cookie,但是当我重新启动应用程序时,NSHTTPCookieStorage 中有相同的 cookie。有时这段代码可以工作,但我想让它每次都能工作。如何解决这个问题?

How to delete cookies in UIWebView? This code:

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

deletes cookies but when I restart my application, there are same cookies in NSHTTPCookieStorage. Sometimes this code works, but i want to make it work everytime.How to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烟花易冷人易散 2024-12-13 10:53:11

尝试这样的事情:

NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
        NSArray* facebookCookies = [cookies cookiesForURL:
                                    [NSURL URLWithString:@"http://login.facebook.com"]];
        for (NSHTTPCookie* cookie in facebookCookies) {
            [cookies deleteCookie:cookie];
        }

Try something like this:

NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
        NSArray* facebookCookies = [cookies cookiesForURL:
                                    [NSURL URLWithString:@"http://login.facebook.com"]];
        for (NSHTTPCookie* cookie in facebookCookies) {
            [cookies deleteCookie:cookie];
        }
孤独陪着我 2024-12-13 10:53:11

这对我有用:

NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];

NSArray *allCookies = [cookies cookies];

for(NSHTTPCookie *cookie in allCookies) {
    if([[cookie domain] rangeOfString:@"facebook.com"].location != NSNotFound) {
        [cookies deleteCookie:cookie];
    }
}

This worked for me:

NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];

NSArray *allCookies = [cookies cookies];

for(NSHTTPCookie *cookie in allCookies) {
    if([[cookie domain] rangeOfString:@"facebook.com"].location != NSNotFound) {
        [cookies deleteCookie:cookie];
    }
}
疯到世界奔溃 2024-12-13 10:53:11

由于某些奇怪的原因,删除单个 cookie 并不总是有效。要真正删除 cookie,您需要存储非特定 cookie,然后重新加载它们,然后迭代所有 cookie 并删除它们,如下所示

NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
   if (cookies != nil && cookies.count > 0) {
       for (NSHTTPCookie *cookie in cookies) {
           [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
       }
       [[NSUserDefaults standardUserDefaults] synchronize];
   }

deleting a single cookie does not always work for some odd reason. To truly get a cookie deleted you will need to store the not specific cookie and then reload them and then iterate through all cookies and delete them such as this

NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
   if (cookies != nil && cookies.count > 0) {
       for (NSHTTPCookie *cookie in cookies) {
           [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
       }
       [[NSUserDefaults standardUserDefaults] synchronize];
   }
献世佛 2024-12-13 10:53:11

与前一个类似但更简单:

NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];

NSArray *allCookies = [cookies cookies];

for(NSHTTPCookie *cookie in allCookies) {
    if([[cookie domain] contains:@"facebook.com"]) {
        [cookies deleteCookie:cookie];
    }
}

“最佳答案”很糟糕,因为它允许删除指定具体 URL 的 cookie。例如,您删除了“login.facebook.com”的 cookie,但您可能会错过“www.login.facebook.com”

The similar for previous one but simplier:

NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];

NSArray *allCookies = [cookies cookies];

for(NSHTTPCookie *cookie in allCookies) {
    if([[cookie domain] contains:@"facebook.com"]) {
        [cookies deleteCookie:cookie];
    }
}

The "best answer" is bad because it allows to delete cookies for the specified concrete URLs. So for example you delete cookie for "login.facebook.com" but you may miss "www.login.facebook.com"

于我来说 2024-12-13 10:53:11

确保最后调用:

[[NSUserDefaults standardUserDefaults]同步];

...就像一个魅力...

Make sure to call:

[[NSUserDefaults standardUserDefaults] synchronize];

in the end... Works like a charm...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文