//获取APP缓存
funcgetCacheSize()-> Double {
// 取出cache文件夹目录
let cachePath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first
// 取出文件夹下所有文件数组
let fileArr = FileManager.default.subpaths(atPath: cachePath!)
//快速枚举出所有文件名 计算文件大小
var size = 0
for file in fileArr! {
// 把文件名拼接到路径中
let path = cachePath! + ("/(file)")
// 取出文件属性
let floder = try! FileManager.default.attributesOfItem(atPath: path)
// 用元组取出文件大小属性
for (key, fileSize) in floder {
// 累加文件大小
if key == FileAttributeKey.size {
size += (fileSize asAnyObject).integerValue
}
}
}
let totalCache = Double(size) /1024.00/1024.00
return totalCache
}
//删除APP缓存
funcclearCache() {
// 取出cache文件夹目录
let cachePath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first
let fileArr = FileManager.default.subpaths(atPath: cachePath!)
// 遍历删除
for file in fileArr! {
let path = (cachePath! asNSString).appending("/(file)")
ifFileManager.default.fileExists(atPath: path) {
do {
tryFileManager.default.removeItem(atPath: path)
} catch {
}
}
}
}