UIWebViewのをクリアしたいアプリが終了すると、URLCache、Javascript(ローカルストレージ)が終了します。Swiftアプリ終了時のWebviewキャッシュをクリアする
私はボタンプレスで次のコードを実装しました。この機能は、ボタンを押すときに意図したとおりに機能します。 AppDelegateのFUNC で
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()
}
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
まったく同じエラーが表示されます。 –