繁体   English   中英

如何清除 iOS swift WKWebview 中的特定缓存

[英]How to clear a specific cache in iOS swift WKWebview

我有一个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)
        }
    }

从循环中我得到了这个

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM