您的位置:首页 > 移动开发 > IOS开发

IOS 开发 Cache文件夹缓存的清理封装(包括WebKit缓存/SDImageCache缓存),都提供了相应的接口.

2016-12-28 17:56 495 查看

ClearCachesTool.h

//
//  ClearCachesTool.h
//  demo
//
//  Created by zhouyu on 2016/12/27.
//  Copyright © 2016年 demo. All rights reserved.
/*
* 真机和模拟器下WKWebKit下载的图片资源缓存路径不一样
* 真机:/Library/Caches/WebKit
* 模拟器:/Library/Caches/cn.demo.demo/WebKit
* cn.demo.demo是指APP的Boundle Identifier
* 真机清理Cache缓存有坑,Snapshots文件夹无操作权限,导致清理失败,提供的方法中已经做了过滤处理
*/

#import <Foundation/Foundation.h>

@interface ClearCachesTool : NSObject

#pragma mark - 计算和清理Cache文件夹总缓存
/*s*
*  获取Cache文件夹缓存的总大小(Snapshots除外,没有权限获取)
*
*  @param path 要获取的文件夹 路径
*
*  @return 返回path路径下文件夹的大小
*/
+ (NSString *)getCacheSize;

/**
*  获取Cache文件夹缓存的总大小(Snapshots除外,没有权限获取)
*  @return 是否清除成功
*/
+ (BOOL)clearCache;
+ (void)readyClearCache;

#pragma mark - 计算和清理指定路径文件夹总缓存
/*s*
*  获取path路径下文件夹的大小
*
*  @param path 要获取的文件夹 路径
*
*  @return 返回path路径下文件夹的大小
*/
+ (NSString *)getCacheSizeWithFilePath:(NSString *)path;

/**
*  清除path路径下文件夹的缓存
*
*  @param path  要清除缓存的文件夹 路径
*
*  @return 是否清除成功
*/
+ (BOOL)clearCacheWithFilePath:(NSString *)path;

#pragma mark - 计算和清理WebKit文件夹的WKWebKit总缓存
/*s*
*  获取WKWebKit路径下文件夹的大小
*  @return 返回WKWebKit路径下文件夹的大小
*/
+ (NSString *)getWKWebKitCacheSize;

/**
*  清除WKWebKit路径下文件夹的缓存
*  @return 是否清除成功
*/
+ (BOOL)clearWKWebKitCache;

#pragma mark - 计算和清理SDImageCache--default文件夹
4000
的总缓存
/*s*
*  获取SDImageCache--default路径下文件夹的大小
*  @return 返回WKWebKit路径下文件夹的大小
*/
+ (NSString *)getSDImageDefaultCacheSize;

/**
*  清除SDImageCache--default路径下文件夹的缓存
*  @return 是否清除成功
*/
+ (BOOL)clearSDImageDefaultCache;

#pragma mark - MAC电脑模拟器下计算和清理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 - 计算和清理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. 拼接每一个文件的全路径
filePath =[path stringByAppendingPathComponent:subPath];
//        ZYLog(@"%@",filePath);
// 2. 是否是文件夹,默认不是
BOOL isDirectory = NO;
// 3. 判断文件是否存在
BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
// 4. 以上判断目的是忽略不需要计算的文件
if (!isExist || isDirectory || [filePath containsString:@".DS"]){
// 过滤: 1. 文件夹不存在  2. 过滤文件夹  3. 隐藏文件
continue;
}
// 5. 指定路径,获取这个路径的属性
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
/**
attributesOfItemAtPath: 文件夹路径
该方法只能获取文件的属性, 无法获取文件夹属性, 所以也是需要遍历文件夹的每一个文件的原因
*/
// 6. 获取每一个文件的大小
NSInteger size = [dict[@"NSFileSize"] integerValue];
// 7. 计算总大小
totleSize += size;
}

//8. 将文件夹大小转换为 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路径的下一级目录的子文件夹
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 - 计算和清理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. 拼接每一个文件的全路径
filePath =[path stringByAppendingPathComponent:subPath];
//        ZYLog(@"%@",filePath);
// 2. 是否是文件夹,默认不是
BOOL isDirectory = NO;
// 3. 判断文件是否存在
BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
// 4. 以上判断目的是忽略不需要计算的文件
if (!isExist || isDirectory || [filePath containsString:@".DS"]){
// 过滤: 1. 文件夹不存在  2. 过滤文件夹  3. 隐藏文件
continue;
}
// 5. 指定路径,获取这个路径的属性
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
/**
attributesOfItemAtPath: 文件夹路径
该方法只能获取文件的属性, 无法获取文件夹属性, 所以也是需要遍历文件夹的每一个文件的原因
*/
// 6. 获取每一个文件的大小
NSInteger size = [dict[@"NSFileSize"] integerValue];
// 7. 计算总大小
totleSize += size;
}

//8. 将文件夹大小转换为 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路径的下一级目录的子文件夹
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文件夹过滤掉,没有访问权限,否则删除时操过200M会失败!!!!!!!!
if (![subPath containsString:@"Snapshots"]) {
// 1. 拼接每一个文件的全路径
filePath =[path stringByAppendingPathComponent:subPath];
}
// 2. 是否是文件夹,默认不是
BOOL isDirectory = NO;
// 3. 判断文件是否存在
BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
// 4. 以上判断目的是忽略不需要计算的文件
if (!isExist || isDirectory || [filePath containsString:@".DS"]){
// 过滤: 1. 文件夹不存在  2. 过滤文件夹  3. 隐藏文件
continue;
}
// 5. 指定路径,获取这个路径的属性
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
/**
attributesOfItemAtPath: 文件夹路径
该方法只能获取文件的属性, 无法获取文件夹属性, 所以也是需要遍历文件夹的每一个文件的原因
*/
// 6. 获取每一个文件的大小
NSInteger size = [dict[@"NSFileSize"] integerValue];
// 7. 计算总大小
totleSize += size;
}

//8. 将文件夹大小转换为 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,真机测试会输出error
//Error Domain=NSCocoaErrorDomain Code=513 "未能移除“Snapshots”,因为您没有访问它的权限。"
+ (BOOL)clearCacheWithFilePath:(NSString *)path{

//拿到path路径的下一级目录的子文件夹
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 - 模拟器下计算和清理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. 拼接每一个文件的全路径
filePath =[path stringByAppendingPathComponent:subPath];
//        ZYLog(@"%@",filePath);
// 2. 是否是文件夹,默认不是
BOOL isDirectory = NO;
// 3. 判断文件是否存在
BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
// 4. 以上判断目的是忽略不需要计算的文件
if (!isExist || isDirectory || [filePath containsString:@".DS"]){
// 过滤: 1. 文件夹不存在  2. 过滤文件夹  3. 隐藏文件
continue;
}
// 5. 指定路径,获取这个路径的属性
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
/**
attributesOfItemAtPath: 文件夹路径
该方法只能获取文件的属性, 无法获取文件夹属性, 所以也是需要遍历文件夹的每一个文件的原因
*/
// 6. 获取每一个文件的大小
NSInteger size = [dict[@"NSFileSize"] integerValue];
// 7. 计算总大小
totleSize += size;
}

//8. 将文件夹大小转换为 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路径的下一级目录的子文件夹
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


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 = @"设置";
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


效果





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: