文章開始前先貼個(gè)圖看下app的沙盒路徑:
網(wǎng)上搜索的清除緩存相關(guān)的文章清一色都有一個(gè)問題,就是只清理
/Library/Caches
目錄文件,理論上來講是沒錯(cuò),所有的緩存文件都會(huì)放在app沙盒目錄的Caches文件目錄下,這樣的清理模擬器能通過,真機(jī)則會(huì)因?yàn)闆]有Snapshot的訪問權(quán)限而中止清理。
所以實(shí)際應(yīng)用中我們的計(jì)算緩存大小函數(shù),以及清理函數(shù)需要跳過Snapshot這個(gè)文件目錄,不講目錄文件大小計(jì)入緩存大小,清理的時(shí)候跳過該目錄防止中斷。
下面的內(nèi)容引用于:
IOS 開發(fā) Cache文件夾緩存的清理封裝(包括WebKit緩存/SDImageCache緩存),都提供了相應(yīng)的接口.
ClearCachesTool.h
//
// ClearCachesTool.h
// demo
//
// Created by zhouyu on 2016/12/27.
// Copyright ? 2016年 demo. All rights reserved.
/*
* 真機(jī)和模擬器下WKWebKit下載的圖片資源緩存路徑不一樣
* 真機(jī):/Library/Caches/WebKit
* 模擬器:/Library/Caches/cn.demo.demo/WebKit
* cn.demo.demo是指APP的Boundle Identifier
* 真機(jī)清理Cache緩存有坑,Snapshots文件夾無操作權(quán)限,導(dǎo)致清理失敗,提供的方法中已經(jīng)做了過濾處理
*/
#import <Foundation/Foundation.h>
@interface ClearCachesTool : NSObject
#pragma mark - 計(jì)算和清理Cache文件夾總緩存
/*s*
* 獲取Cache文件夾緩存的總大小(Snapshots除外,沒有權(quán)限獲取)
*
* @param path 要獲取的文件夾 路徑
*
* @return 返回path路徑下文件夾的大小
*/
+ (NSString *)getCacheSize;
/**
* 獲取Cache文件夾緩存的總大小(Snapshots除外,沒有權(quán)限獲取)
* @return 是否清除成功
*/
+ (BOOL)clearCache;
+ (void)readyClearCache;
#pragma mark - 計(jì)算和清理指定路徑文件夾總緩存
/*s*
* 獲取path路徑下文件夾的大小
*
* @param path 要獲取的文件夾 路徑
*
* @return 返回path路徑下文件夾的大小
*/
+ (NSString *)getCacheSizeWithFilePath:(NSString *)path;
/**
* 清除path路徑下文件夾的緩存
*
* @param path 要清除緩存的文件夾 路徑
*
* @return 是否清除成功
*/
+ (BOOL)clearCacheWithFilePath:(NSString *)path;
#pragma mark - 計(jì)算和清理WebKit文件夾的WKWebKit總緩存
/*s*
* 獲取WKWebKit路徑下文件夾的大小
* @return 返回WKWebKit路徑下文件夾的大小
*/
+ (NSString *)getWKWebKitCacheSize;
/**
* 清除WKWebKit路徑下文件夾的緩存
* @return 是否清除成功
*/
+ (BOOL)clearWKWebKitCache;
#pragma mark - 計(jì)算和清理SDImageCache--default文件夾的總緩存
/*s*
* 獲取SDImageCache--default路徑下文件夾的大小
* @return 返回WKWebKit路徑下文件夾的大小
*/
+ (NSString *)getSDImageDefaultCacheSize;
/**
* 清除SDImageCache--default路徑下文件夾的緩存
* @return 是否清除成功
*/
+ (BOOL)clearSDImageDefaultCache;
#pragma mark - MAC電腦模擬器下計(jì)算和清理WebKit文件夾的WKWebKit總緩存
/*s*
* 獲取模擬器下WKWebKit路徑下文件夾的大小
* @return 返回WKWebKit路徑下文件夾的大小
*/
+ (NSString *)getSimulatorWKWebKitCacheSize;
/**
* 清除模擬器下WKWebKit路徑下文件夾的緩存
* @return 是否清除成功
*/
+ (BOOL)clearSimulatorWKWebKitCache;
@end
ClearCachesTool.m
//
// ClearCachesTool.m
// demo
//
// Created by zhouyu on 2016/12/27.
// Copyright ? 2016年 demo. All rights reserved.
//
#import "ClearCachesTool.h"
@implementation ClearCachesTool
#pragma mark - 獲取Cache文件夾大小
+ (NSString *)getCacheSize {
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSString *totleSize = [ClearCachesTool getCacheSizeWithFilePath:cachesPath];
return totleSize;
}
#pragma mark - 刪除Cache文件夾中的緩存
+ (BOOL)clearCache {
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
BOOL isAlreadyClearCache = [ClearCachesTool clearCacheWithFilePath:cachesPath];
return isAlreadyClearCache;
}
+ (void)readyClearCache {
BOOL isClearCache = [ClearCachesTool clearCache];
if (isClearCache) {
ZYLog(@"清理完畢");
} else {
ZYLog(@"清理失敗");
}
}
#pragma mark - 計(jì)算和清理WebKit文件夾的WKWebKit總緩存
+ (NSString *)getWKWebKitCacheSize {
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSString *path = [cachesPath stringByAppendingPathComponent:@"/WebKit"];
// 獲取“path”文件夾下的所有文件
NSArray *subPathArr = [[NSFileManager defaultManager] subpathsAtPath:path];
NSString *filePath = nil;
NSInteger totleSize = 0;
for (NSString *subPath in subPathArr){
// 1. 拼接每一個(gè)文件的全路徑
filePath =[path stringByAppendingPathComponent:subPath];
// ZYLog(@"%@",filePath);
// 2. 是否是文件夾,默認(rèn)不是
BOOL isDirectory = NO;
// 3. 判斷文件是否存在
BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
// 4. 以上判斷目的是忽略不需要計(jì)算的文件
if (!isExist || isDirectory || [filePath containsString:@".DS"]){
// 過濾: 1. 文件夾不存在 2. 過濾文件夾 3. 隱藏文件
continue;
}
// 5. 指定路徑,獲取這個(gè)路徑的屬性
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
/**
attributesOfItemAtPath: 文件夾路徑
該方法只能獲取文件的屬性, 無法獲取文件夾屬性, 所以也是需要遍歷文件夾的每一個(gè)文件的原因
*/
// 6. 獲取每一個(gè)文件的大小
NSInteger size = [dict[@"NSFileSize"] integerValue];
// 7. 計(jì)算總大小
totleSize += size;
}
//8. 將文件夾大小轉(zhuǎn)換為 M/KB/B
NSString *totleStr = nil;
if (totleSize > 1000 * 1000){
totleStr = [NSString stringWithFormat:@"%.2fM",totleSize / 1000.00f /1000.00f];
}else if (totleSize > 1000){
totleStr = [NSString stringWithFormat:@"%.2fKB",totleSize / 1000.00f ];
}else{
totleStr = [NSString stringWithFormat:@"%.2fB",totleSize / 1.00f];
}
return totleStr;
}
+ (BOOL)clearWKWebKitCache {
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSString *path = [cachesPath stringByAppendingPathComponent:@"/WebKit"];
//拿到path路徑的下一級(jí)目錄的子文件夾
NSArray *subPathArr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
NSString *filePath = nil;
NSError *error = nil;
for (NSString *subPath in subPathArr) {
filePath = [path stringByAppendingPathComponent:subPath];
// ZYLog(@"%@",filePath);
if (![filePath containsString:@"/Caches/Snapshots"]) {
//刪除子文件夾
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
}
if (error) {
ZYLog(@"%@",error);
return NO;
}
}
return YES;
}
#pragma mark - 計(jì)算和清理SDImageCache--default文件夾的總緩存
+ (NSString *)getSDImageDefaultCacheSize {
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSString *path = [cachesPath stringByAppendingPathComponent:@"/default"];
// 獲取“path”文件夾下的所有文件
NSArray *subPathArr = [[NSFileManager defaultManager] subpathsAtPath:path];
NSString *filePath = nil;
NSInteger totleSize = 0;
for (NSString *subPath in subPathArr){
// 1. 拼接每一個(gè)文件的全路徑
filePath =[path stringByAppendingPathComponent:subPath];
// ZYLog(@"%@",filePath);
// 2. 是否是文件夾,默認(rèn)不是
BOOL isDirectory = NO;
// 3. 判斷文件是否存在
BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
// 4. 以上判斷目的是忽略不需要計(jì)算的文件
if (!isExist || isDirectory || [filePath containsString:@".DS"]){
// 過濾: 1. 文件夾不存在 2. 過濾文件夾 3. 隱藏文件
continue;
}
// 5. 指定路徑,獲取這個(gè)路徑的屬性
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
/**
attributesOfItemAtPath: 文件夾路徑
該方法只能獲取文件的屬性, 無法獲取文件夾屬性, 所以也是需要遍歷文件夾的每一個(gè)文件的原因
*/
// 6. 獲取每一個(gè)文件的大小
NSInteger size = [dict[@"NSFileSize"] integerValue];
// 7. 計(jì)算總大小
totleSize += size;
}
//8. 將文件夾大小轉(zhuǎn)換為 M/KB/B
NSString *totleStr = nil;
if (totleSize > 1000 * 1000){
totleStr = [NSString stringWithFormat:@"%.2fM",totleSize / 1000.00f /1000.00f];
}else if (totleSize > 1000){
totleStr = [NSString stringWithFormat:@"%.2fKB",totleSize / 1000.00f ];
}else{
totleStr = [NSString stringWithFormat:@"%.2fB",totleSize / 1.00f];
}
return totleStr;
}
+ (BOOL)clearSDImageDefaultCache {
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSString *path = [cachesPath stringByAppendingPathComponent:@"/default"];
//拿到path路徑的下一級(jí)目錄的子文件夾
NSArray *subPathArr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
NSString *filePath = nil;
NSError *error = nil;
for (NSString *subPath in subPathArr) {
filePath = [path stringByAppendingPathComponent:subPath];
// ZYLog(@"%@",filePath);
if (![filePath containsString:@"/Caches/Snapshots"]) {
//刪除子文件夾
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
}
if (error) {
ZYLog(@"%@",error);
return NO;
}
}
return YES;
}
#pragma mark - 獲取path路徑下文件夾大小
+ (NSString *)getCacheSizeWithFilePath:(NSString *)path {
// 獲取“path”文件夾下的所有文件
NSArray *subPathArr = [[NSFileManager defaultManager] subpathsAtPath:path];
NSString *filePath = nil;
NSInteger totleSize = 0;
for (NSString *subPath in subPathArr){
// ZYLog(@"getCacheSize = %@",subPath);
// 0. 把Snapshots文件夾過濾掉,沒有訪問權(quán)限,否則刪除時(shí)操過200M會(huì)失敗!!!!!!!!
if (![subPath containsString:@"Snapshots"]) {
// 1. 拼接每一個(gè)文件的全路徑
filePath =[path stringByAppendingPathComponent:subPath];
}
// 2. 是否是文件夾,默認(rèn)不是
BOOL isDirectory = NO;
// 3. 判斷文件是否存在
BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
// 4. 以上判斷目的是忽略不需要計(jì)算的文件
if (!isExist || isDirectory || [filePath containsString:@".DS"]){
// 過濾: 1. 文件夾不存在 2. 過濾文件夾 3. 隱藏文件
continue;
}
// 5. 指定路徑,獲取這個(gè)路徑的屬性
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
/**
attributesOfItemAtPath: 文件夾路徑
該方法只能獲取文件的屬性, 無法獲取文件夾屬性, 所以也是需要遍歷文件夾的每一個(gè)文件的原因
*/
// 6. 獲取每一個(gè)文件的大小
NSInteger size = [dict[@"NSFileSize"] integerValue];
// 7. 計(jì)算總大小
totleSize += size;
}
//8. 將文件夾大小轉(zhuǎn)換為 M/KB/B
NSString *totleStr = nil;
if (totleSize > 1000 * 1000){
totleStr = [NSString stringWithFormat:@"%.2fM",totleSize / 1000.00f /1000.00f];
}else if (totleSize > 1000){
totleStr = [NSString stringWithFormat:@"%.2fKB",totleSize / 1000.00f ];
}else{
totleStr = [NSString stringWithFormat:@"%.2fB",totleSize / 1.00f];
}
return totleStr;
}
#pragma mark - 清除path文件夾下緩存大小--/Caches/Snapshots,真機(jī)測(cè)試會(huì)輸出error
//Error Domain=NSCocoaErrorDomain Code=513 "未能移除“Snapshots”,因?yàn)槟鷽]有訪問它的權(quán)限。"
+ (BOOL)clearCacheWithFilePath:(NSString *)path{
//拿到path路徑的下一級(jí)目錄的子文件夾
NSArray *subPathArr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
NSString *filePath = nil;
NSError *error = nil;
for (NSString *subPath in subPathArr) {
filePath = [path stringByAppendingPathComponent:subPath];
// ZYLog(@"%@",filePath);
if (![filePath containsString:@"/Caches/Snapshots"]) {
//刪除子文件夾
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
}
if (error) {
ZYLog(@"%@",error);
return NO;
}
}
return YES;
}
#pragma mark - 模擬器下計(jì)算和清理WebKit文件夾的WKWebKit總緩存
+ (NSString *)getSimulatorWKWebKitCacheSize {
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSString *identifier = [[NSBundle mainBundle] bundleIdentifier];
// ZYLog(@"%@",identifier);
NSString *path = [NSString stringWithFormat:@"%@/%@",cachesPath,identifier];
path = [path stringByAppendingPathComponent:@"/WebKit"];
// ZYLog(@"%@",path);
// 獲取“path”文件夾下的所有文件
NSArray *subPathArr = [[NSFileManager defaultManager] subpathsAtPath:path];
NSString *filePath = nil;
NSInteger totleSize = 0;
for (NSString *subPath in subPathArr){
// 1. 拼接每一個(gè)文件的全路徑
filePath =[path stringByAppendingPathComponent:subPath];
// ZYLog(@"%@",filePath);
// 2. 是否是文件夾,默認(rèn)不是
BOOL isDirectory = NO;
// 3. 判斷文件是否存在
BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
// 4. 以上判斷目的是忽略不需要計(jì)算的文件
if (!isExist || isDirectory || [filePath containsString:@".DS"]){
// 過濾: 1. 文件夾不存在 2. 過濾文件夾 3. 隱藏文件
continue;
}
// 5. 指定路徑,獲取這個(gè)路徑的屬性
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
/**
attributesOfItemAtPath: 文件夾路徑
該方法只能獲取文件的屬性, 無法獲取文件夾屬性, 所以也是需要遍歷文件夾的每一個(gè)文件的原因
*/
// 6. 獲取每一個(gè)文件的大小
NSInteger size = [dict[@"NSFileSize"] integerValue];
// 7. 計(jì)算總大小
totleSize += size;
}
//8. 將文件夾大小轉(zhuǎn)換為 M/KB/B
NSString *totleStr = nil;
if (totleSize > 1000 * 1000){
totleStr = [NSString stringWithFormat:@"%.2fM",totleSize / 1000.00f /1000.00f];
}else if (totleSize > 1000){
totleStr = [NSString stringWithFormat:@"%.2fKB",totleSize / 1000.00f ];
}else{
totleStr = [NSString stringWithFormat:@"%.2fB",totleSize / 1.00f];
}
return totleStr;
}
+ (BOOL)clearSimulatorWKWebKitCache {
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSString *identifier = [[NSBundle mainBundle] bundleIdentifier];
// ZYLog(@"%@",identifier);
NSString *path = [NSString stringWithFormat:@"%@/%@",cachesPath,identifier];
path = [path stringByAppendingPathComponent:@"/WebKit"];
// ZYLog(@"%@",path);
//拿到path路徑的下一級(jí)目錄的子文件夾
NSArray *subPathArr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
NSString *filePath = nil;
NSError *error = nil;
for (NSString *subPath in subPathArr) {
filePath = [path stringByAppendingPathComponent:subPath];
// ZYLog(@"%@",filePath);
if (![filePath containsString:@"/Caches/Snapshots"]) {
//刪除子文件夾
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
}
if (error) {
ZYLog(@"%@",error);
return NO;
}
}
return YES;
}
@end
使用DEMO:
WOCOSetMyInfosController.m
//
// WOCOSetMyInfosController.m
// demo
//
// Created by zhouyu on 2016/12/27.
// Copyright ? 2016年 demo. All rights reserved.
//
#import "WOCOSetMyInfosController.h"
#import "ClearCachesTool.h"
#import "SVProgressHUD.h"
@interface WOCOSetMyInfosController ()
/**
* caches文件夾下緩存大小
*/
@property (nonatomic, copy) NSString *cachesSize;
@end
@implementation WOCOSetMyInfosController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"設(shè)置";
self.tableView.backgroundColor = [UIColor whiteColor];
self.tableView.scrollEnabled = NO;
[self getCachesSize];
}
#pragma mark - 獲取Caches文件夾緩存大小
- (void)getCachesSize {
// 子線程計(jì)算文件緩存
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSString *cachesSize = [ClearCachesTool getCacheSize];
self.cachesSize = cachesSize;
// 主線程更新顯示
dispatch_async(dispatch_get_main_queue(), ^{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.detailTextLabel.text = cachesSize;
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
});
});
}
#pragma mark - 刪除Caches文件夾緩存
- (void)clearCaches {
// 子線程做刪除耗時(shí)操作
if (self.cachesSize == nil) {
return;
}
NSString *message = [NSString stringWithFormat:@"緩存大小為%@,確定要清理嗎?",self.cachesSize];
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *certain = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[SVProgressHUD showWithStatus:@"正在拼命清理中..."];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[ClearCachesTool clearCache];
// 主線程刷新顯示
dispatch_async(dispatch_get_main_queue(), ^{
[SVProgressHUD dismiss];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
self.cachesSize = nil;
cell.detailTextLabel.text = @"清理完畢";
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
});
});
}];
[alertC addAction:cancel];
[alertC addAction:certain];
[self presentViewController:alertC animated:YES completion:nil];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.row == 0) {
[self clearCaches];
}
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
}
if (indexPath.section == 0 && indexPath.row == 0) {
cell.textLabel.text = @"清理緩存";
cell.detailTextLabel.text = @"正在計(jì)算中...";
}else {
cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.section];
cell.detailTextLabel.text = @"^_^";
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
@end