#import <Foundation/Foundation.h>
@interface ClearCachesTool : NSObject
#pragma mark - 计算和清理Cache文件夹总缓存
+ (NSString *)getCacheSize;
/**
* 获取Cache文件夹缓存的总大小(Snapshots除外,没有权限获取)
* @return 是否清除成功
*/
+ (BOOL)clearCache;
+ (void)readyClearCache;
#pragma mark - 计算和清理指定路径文件夹总缓存
+ (NSString *)getCacheSizeWithFilePath:(NSString *)path;
/**
* 清除path路径下文件夹的缓存
*
* @param path 要清除缓存的文件夹 路径
*
* @return 是否清除成功
*/
+ (BOOL)clearCacheWithFilePath:(NSString *)path;
#pragma mark - 计算和清理WebKit文件夹的WKWebKit总缓存
+ (NSString *)getWKWebKitCacheSize;
/**
* 清除WKWebKit路径下文件夹的缓存
* @return 是否清除成功
*/
+ (BOOL)clearWKWebKitCache;
#pragma mark - 计算和清理SDImageCache--default文件夹的总缓存
+ (NSString *)getSDImageDefaultCacheSize;
/**
* 清除SDImageCache--default路径下文件夹的缓存
* @return 是否清除成功
*/
+ (BOOL)clearSDImageDefaultCache;
#pragma mark - MAC电脑模拟器下计算和清理WebKit文件夹的WKWebKit总缓存
+ (NSString *)getSimulatorWKWebKitCacheSize;
/**
* 清除模拟器下WKWebKit路径下文件夹的缓存
* @return 是否清除成功
*/
+ (BOOL)clearSimulatorWKWebKitCache;
@end
#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 - 计算和清理WebKit文件夹的WKWebKit总缓存
+ (NSString *)getWKWebKitCacheSize {
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSString *path = [cachesPath stringByAppendingPathComponent:@"/WebKit"];
NSArray *subPathArr = [[NSFileManager defaultManager] subpathsAtPath:path];
NSString *filePath = nil;
NSInteger totleSize = 0;
for (NSString *subPath in subPathArr){
filePath =[path stringByAppendingPathComponent:subPath];
BOOL isDirectory = NO;
BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
if (!isExist || isDirectory || [filePath containsString:@".DS"]){
continue;
}
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
NSInteger size = [dict[@"NSFileSize"] integerValue];
totleSize += size;
}
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"];
NSArray *subPathArr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
NSString *filePath = nil;
NSError *error = nil;
for (NSString *subPath in subPathArr) {
filePath = [path stringByAppendingPathComponent:subPath];
if (![filePath containsString:@"/Caches/Snapshots"]) {
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
}
if (error) {
ZYLog(@"%@",error);
return NO;
}
}
return YES;
}
#pragma mark - 计算和清理SDImageCache--default文件夹的总缓存
+ (NSString *)getSDImageDefaultCacheSize {
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSString *path = [cachesPath stringByAppendingPathComponent:@"/default"];
NSArray *subPathArr = [[NSFileManager defaultManager] subpathsAtPath:path];
NSString *filePath = nil;
NSInteger totleSize = 0;
for (NSString *subPath in subPathArr){
filePath =[path stringByAppendingPathComponent:subPath];
BOOL isDirectory = NO;
BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
if (!isExist || isDirectory || [filePath containsString:@".DS"]){
continue;
}
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
NSInteger size = [dict[@"NSFileSize"] integerValue];
totleSize += size;
}
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"];
NSArray *subPathArr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
NSString *filePath = nil;
NSError *error = nil;
for (NSString *subPath in subPathArr) {
filePath = [path stringByAppendingPathComponent:subPath];
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 {
NSArray *subPathArr = [[NSFileManager defaultManager] subpathsAtPath:path];
NSString *filePath = nil;
NSInteger totleSize = 0;
for (NSString *subPath in subPathArr){
if (![subPath containsString:@"Snapshots"]) {
filePath =[path stringByAppendingPathComponent:subPath];
}
BOOL isDirectory = NO;
BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
if (!isExist || isDirectory || [filePath containsString:@".DS"]){
continue;
}
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
NSInteger size = [dict[@"NSFileSize"] integerValue];
totleSize += size;
}
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,真机测试会输出error
+ (BOOL)clearCacheWithFilePath:(NSString *)path{
NSArray *subPathArr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
NSString *filePath = nil;
NSError *error = nil;
for (NSString *subPath in subPathArr) {
filePath = [path stringByAppendingPathComponent:subPath];
if (![filePath containsString:@"/Caches/Snapshots"]) {
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
}
if (error) {
ZYLog(@"%@",error);
return NO;
}
}
return YES;
}
#pragma mark - 模拟器下计算和清理WebKit文件夹的WKWebKit总缓存
+ (NSString *)getSimulatorWKWebKitCacheSize {
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSString *identifier = [[NSBundle mainBundle] bundleIdentifier];
NSString *path = [NSString stringWithFormat:@"%@/%@",cachesPath,identifier];
path = [path stringByAppendingPathComponent:@"/WebKit"];
NSArray *subPathArr = [[NSFileManager defaultManager] subpathsAtPath:path];
NSString *filePath = nil;
NSInteger totleSize = 0;
for (NSString *subPath in subPathArr){
filePath =[path stringByAppendingPathComponent:subPath];
BOOL isDirectory = NO;
BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
if (!isExist || isDirectory || [filePath containsString:@".DS"]){
continue;
}
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
NSInteger size = [dict[@"NSFileSize"] integerValue];
totleSize += size;
}
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];
NSString *path = [NSString stringWithFormat:@"%@/%@",cachesPath,identifier];
path = [path stringByAppendingPathComponent:@"/WebKit"];
NSArray *subPathArr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
NSString *filePath = nil;
NSError *error = nil;
for (NSString *subPath in subPathArr) {
filePath = [path stringByAppendingPathComponent:subPath];
if (![filePath containsString:@"/Caches/Snapshots"]) {
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
}
if (error) {
ZYLog(@"%@",error);
return NO;
}
}
return YES;
}
@end
WOCOSetMyInfosController.m
#import "WOCOSetMyInfosController.h"
#import "ClearCachesTool.h"
#import "SVProgressHUD.h"
@interface WOCOSetMyInfosController ()
@property (nonatomic, copy) NSString *cachesSize;
@end
@implementation WOCOSetMyInfosController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"设置";
self.tableView.backgroundColor = [UIColor whiteColor];
self.tableView.scrollEnabled = NO;
[self getCachesSize];
}
#pragma mark - 获取Caches文件夹缓存大小
- (void)getCachesSize {
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 {
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 = @"正在计算中...";
}else {
cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.section];
cell.detailTextLabel.text = @"^_^";
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
@end
效果