【问题标题】:How to clear a specific cache in iOS swift WKWebview如何在 iOS swift WKWebview 中清除特定缓存
【发布时间】:2020-08-22 23:03:35
【问题描述】:

我有一个WKWebView 应用程序,我想在上传图片时清除特定缓存。

当我上传图像时,名称保持不变,但旧图像将被新图像覆盖,例如旧图像是logo.png,新图像也将被重命名为logo.png。但是由于缓存用户仍然会看到旧图像。

一旦图像上传并且工作正常,我有下面的代码行来清除缓存,但我现在唯一的问题是所有缓存的数据也被清除了。

有什么方法可以清除仅上传图像的缓存,也许是通过传递图像名称?

或者只是清除图片的缓存?

static func clearCache(){
        let cacheURL =  FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
        let fileManager = FileManager.default
        do {
            // Get the directory contents urls (including subfolders urls)
            let directoryContents = try FileManager.default.contentsOfDirectory( at: cacheURL, includingPropertiesForKeys: nil, options: [])
            for file in directoryContents {
                print("CACHE = ", file)
                do {
                    try fileManager.removeItem(at: file)
                }
                catch let error as NSError {
                    debugPrint("Ooops! Something went wrong: \(error)")
                }

            }
        } catch let error as NSError {
            print(error.localizedDescription)
        }
    }

从循环中我得到了这个

缓存 = file:///private/var/mobile/Containers/Data/Application/BFEB788B-9EA4-4921-A902-230869CAC814/Library/Caches/com.myapp.ios.sellers/ 缓存 = file:///private/var/mobile/Containers/Data/Application/BFEB788B-9EA4-4921-A902-230869CAC814/Library/Caches/google-sdks-events/ 缓存 = file:///private/var/mobile/Containers/Data/Application/BFEB788B-9EA4-4921-A902-230869CAC814/Library/Caches/com.apple.WebKit.WebContent/ 缓存 = file:///private/var/mobile/Containers/Data/Application/BFEB788B-9EA4-4921-A902-230869CAC814/Library/Caches/WebKit/

【问题讨论】:

    标签: ios swift caching wkwebview


    【解决方案1】:

    当您处于文件的 for 循环循环中时,您似乎需要找到要删除的文件。当 for 循环命中时删除该特定图像。现在 for 循环会删除 directoryContents 中的所有缓存。

    【讨论】:

    • 我用从循环中得到的内容更新了我的问题。如何获取图像?
    猜你喜欢
    • 2020-02-08
    • 1970-01-01
    • 2019-09-29
    • 2015-01-22
    • 2019-03-08
    • 2020-05-31
    • 1970-01-01
    • 2012-02-05
    • 2015-03-22
    相关资源
    最近更新 更多