我使用NSURLRequestReturnCacheDataElseLoad
缓存策略将网页加载到WKWebview。除非 服务器明确告诉我这样做,否则不需要我清除缓存。但是,一旦服务器告诉我要执行此操作,我将无法清除缓存它。在iOS中清除缓存目录的最佳做法是什么?
大多数的答案和文章表明,removeAllCachedResponses
作品,但也有一些抱怨周围 循环约NSURLCache不NSURLSession或UIWebView的。我正常工作无法得到它,我无论是在iOS的8.4或9.3的模拟器工作。
所以我用下面的代码以编程方式清除缓存目录中的所有文件。我在我的WKWebview 中使用的网站的缓存文件位于Application/Cache/bundleidentifier。尽管如此,我尝试删除所有可用的文件。当我运行代码时,出现错误,试图删除/快照 。现在这让我想知道缓存目录中有哪些其他文件不应该被篡改? 我知道SDWebImage缓存和其他几个文件驻留在此目录中。但是,我需要清除SDWebImage缓存。
这里是我用来清除缓存目录代码:
public func clearCache(){
let cacheURL = NSFileManager.defaultManager().URLsForDirectory(.CachesDirectory, inDomains: .UserDomainMask).first!
let fileManager = NSFileManager.defaultManager()
do {
// Get the directory contents urls (including subfolders urls)
let directoryContents = try NSFileManager.defaultManager().contentsOfDirectoryAtURL(cacheURL, includingPropertiesForKeys: nil, options: [])
for file in directoryContents {
do {
try fileManager.removeItemAtURL(file)
}
catch let error as NSError {
debugPrint("Ooops! Something went wrong: \(error)")
}
}
} catch let error as NSError {
print(error.localizedDescription)
}
}
现在,这是一个好的做法呢?是否有任何明显的方法,我失踪实现相同?