我想清除的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
在那裏得到完全相同的錯誤。 –