【发布时间】:2016-12-31 07:37:57
【问题描述】:
我想在应用终止时清除 UIWebView 的 URLCache, Javascript(local storage)。
我在按下按钮时实现了以下代码。该功能在按下按钮时按预期工作。
func clearEverything() {
print("cleared")
URLCache.shared.removeAllCachedResponses()
URLCache.shared.diskCapacity = 0
URLCache.shared.memoryCapacity = 0
webView.stringByEvaluatingJavaScript(from: "localStorage.clear();")
let cookieJar = HTTPCookieStorage.shared
for cookie in cookieJar.cookies! {
cookieJar.deleteCookie(cookie)
}
UserDefaults.standard.synchronize()
}
在AppDelegate的func applicationWillTerminate中,我使用了如下代码:
func applicationWillTerminate(_ application: UIApplication) {
print("app terminated")
clearEverything()
}
func clearEverything() {
print("cleared from App Delegate")
URLCache.shared.removeAllCachedResponses()
URLCache.shared.diskCapacity = 0
URLCache.shared.memoryCapacity = 0
ViewController().webView.stringByEvaluatingJavaScript(from: "localStorage.clear();")
let cookieJar = HTTPCookieStorage.shared
for cookie in cookieJar.cookies! {
cookieJar.deleteCookie(cookie)
}
UserDefaults.standard.synchronize()
}
每当我终止应用程序时,我都会收到以下错误:
建议我在不使应用程序崩溃的情况下实现此功能的最佳方法。
谢谢!!
【问题讨论】:
-
你为什么不尝试在 didFinishLaunchingWithOptions 中清除它?
-
那里得到完全相同的错误。
标签: javascript ios uiwebview swift3