我想清除的UIWebView的当应用终止时,URLCache,Javascript(本地存储)。Swift在应用终止时清除webview缓存
我在一个按钮上按下了以下代码。该功能在按钮按下时按预期工作。
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中清除它? – iYoung
在那里得到完全相同的错误。 –