import Foundation
import WebKit
class ClearCacheManage {
class func sizeOfAllCache() -> String {
let cachePath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first
let fileArr = FileManager.default.subpaths(atPath: cachePath!)
var size: Double = 0
for file in fileArr! {
let path = (cachePath! as NSString).appending("/\(file)")
let floder = try! FileManager.default.attributesOfItem(atPath: path)
for (abc, bcd) in floder {
if abc == FileAttributeKey.size {
size += (bcd as AnyObject).doubleValue
}
}
}
let cache = size / 1024 / 1024
return String(format: "%.1fM", cache)
}
class func clearAllCache() {
let cachePath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first
let fileArr = FileManager.default.subpaths(atPath: cachePath!)
for file in fileArr! {
let path = (cachePath! as NSString).appending("/\(file)")
if FileManager.default.fileExists(atPath: path) {
do {
try FileManager.default.removeItem(atPath: path)
} catch {}
}
}
}
class func clearBrowserCache() {
let dataStore = WKWebsiteDataStore.default()
dataStore.fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(), completionHandler: { (records) in
for record in records{
WKWebsiteDataStore.default().removeData(ofTypes: record.dataTypes, for: [record], completionHandler: {
print("清除成功\(record)")
})
}
})
}
class func sizeOfBrowserCache() -> String {
return ""
}
}
Swift清除APP缓存和WebView缓存
最新推荐文章于 2024-09-24 16:38:10 发布