【发布时间】: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