【问题标题】:Delete All Cache and Cookies, Reset an Entire App删除所有缓存和 Cookie,重置整个应用程序
【发布时间】:2016-03-07 12:34:56
【问题描述】:

我正在尝试构建一个具有 UIWebView 的应用程序。当我退出我的应用程序时,我希望重置所有数据,包括缓存和 cookie。

我目前能够删除 cookie(可能还有缓存),但问题是即使在删除之后,一些网站似乎还有以前活动的数据。

这是我用来清除 cookie 和缓存的代码:

// Loops through each of the cookies and deletes them.
for cookie in NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies! {
    NSHTTPCookieStorage.sharedHTTPCookieStorage().deleteCookie(cookie)
}
NSUserDefaults.standardUserDefaults().synchronize()

// Removes cache for all responses
NSURLCache.sharedURLCache().removeAllCachedResponses()

这似乎适用于大多数网站,但即使在删除 cookie 并删除所有缓存之后,Google 也不会停止显示我的搜索历史记录。我没有登录 Google。

如果有任何帮助,我将不胜感激!

示例截图:Google Search History

【问题讨论】:

标签: swift uiwebview nsurlcache nshttpcookiestorage


【解决方案1】:

我终于(在尝试了两天之后)找到了解决这个问题的方法。我不得不在 UIWebView 中使用 JavaScript 清理本地存储。

yourWebView.stringByEvaluatingJavaScriptFromString("localStorage.clear();")!

我发布这篇文章是希望有人不必花两天时间来解决这个问题!

【讨论】:

  • 我试过 webView.evaluateJavaScript("localStorage.clear();") 但 id 不起作用。触发 JS 异常
  • 不错,但我们不需要!最后它在 swift 3 中为我工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-08
  • 1970-01-01
  • 1970-01-01
  • 2015-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多