91

在我的应用程序中,我有一个UIWebview加载linkedin auth 登录页面。当用户登录时,cookie 会保存到应用程序中。

我的应用程序有一个与linkedin 登录无关的注销按钮。因此,当用户单击此按钮时,他会从应用程序中注销。我希望此注销也会从应用程序中清除他的linkedin cookie,以便用户完全注销。

4

5 回答 5

210

根据这个问题,您可以遍历“Cookie Jar”中的每个 cookie 并删除它们,如下所示:

NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [storage cookies]) {
   [storage deleteCookie:cookie];
}
[[NSUserDefaults standardUserDefaults] synchronize];
于 2010-12-17T15:03:08.900 回答
8

只是想添加一些有关此的信息。

OS X 10.9 / iOS 7及更高版本中,您可以使用-resetWithCompletionHandler:从您的以下内容中清除整个应用程序的 cookie 和缓存等sharedSession

清空所有 cookie、缓存和凭证存储,删除磁盘文件,将正在进行的下载刷新到磁盘,并确保未来的请求发生在新的套接字上。

[[NSURLSession sharedSession] resetWithCompletionHandler:^{
    // Do something once it's done.
}];

for-In 循环deleteCookie:听起来像是在向我枚举集合时进行修改。(不知道,可能是个坏主意?)

于 2016-04-05T11:06:10.370 回答
1

您可以在 WebView 的 html 中创建一个函数来清理 cookie。

如果您只需要清洁一次,您可以使用 Titanium 事件触发此功能,仅当应用程序启动时。

于 2012-04-21T11:48:32.833 回答
1

如果有人正在寻找 Swift 解决方案:

    let storage = HTTPCookieStorage.shared
    if let cookies = storage.cookies{
        for cookie in cookies {
             storage.deleteCookie(cookie)
        }
    }
于 2019-06-27T08:30:38.520 回答
1

在使用 MKWebView 的情况下,以前的答案对我没有帮助。所以,我找到了另一个解决方案:

func loadAuthUrl(_ url: URL) {
        
        let finally: VoidClosure = { [weak self] in
            guard let self = self else { return }
            let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 60.0)
            self.webView.load(request)
        }
        
        let cookieStore = webView.configuration.websiteDataStore.httpCookieStore
        cookieStore.getAllCookies({ [weak self] cookies in
            guard let _ = self else { return }
            let instagramCookies = cookies.filter({ $0.domain == ".instagram.com" })
            if instagramCookies.isEmpty {
                finally()
            } else {
                DispatchQueue.global().async(execute: {
                    let group = DispatchGroup()
                    for cookie in cookies {
                        group.enter()
                        cookieStore.delete(cookie, completionHandler: { group.leave() })
                    }
                    group.wait()
                    DispatchQueue.main.async(execute: finally)
                })
            }
        })
    }
于 2022-02-01T12:38:26.630 回答