現(xiàn)在很多app都有清除緩存的功能,下面是之前封裝好的全部代碼,只需要一句話就可以搞定清除緩存
.h文件
// 文件操作以及屬性獲取等方法
#import <Foundation/Foundation.h>
@interface NSObject (FileManager)
/* 計(jì)算某個文件或者文件夾的大小
path:文件或者文件夾路徑
completion:計(jì)算完成回調(diào),計(jì)算完成就會調(diào)用這個block,并且把計(jì)算的大小傳出去
*/
+ (void)getFileCacheSizeWithPath:(NSString *)path completion:(void(^)(NSInteger totalSize))completion;
- (void)getFileCacheSizeWithPath:(NSString *)path completion:(void(^)(NSInteger totalSize))completion;
// 獲取沙盒中cache文件夾的路徑
- (NSString *)cachePath;
+ (NSString *)cachePath;
// 刪除沙盒中cache文件夾里面的緩存(文件)
- (void)removeCacheWithCompletion:(void(^)())completion;
+ (void)removeCacheWithCompletion:(void(^)())completion;
@end
.m文件
#import "NSObject+FileManager.h"
@implementation NSObject (FileManager)
- (NSString *)cachePath
{
return [NSObject cachePath];
}
+ (NSString *)cachePath
{
// 獲取cachePath文件路徑
return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
}
- (void)getFileCacheSizeWithPath:(NSString *)path completion:(void (^)(NSInteger))completion
{
[NSObject getFileCacheSizeWithPath:path completion:completion];
}
// 異步方法,不需要返回值,異步方法使用回調(diào)block傳遞返回值
// 獲取文件尺寸
+ (void)getFileCacheSizeWithPath:(NSString *)path completion:(void(^)(NSInteger totalSize))completion
{
// 文件操作比較耗時,開啟子線程去操作,然后在返回
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// 1.創(chuàng)建文件管理者
NSFileManager *mgr = [NSFileManager defaultManager];
// 1.1.判斷下是否存在,而且是否是文件夾
BOOL isDirectory;
BOOL isFileExist = [mgr fileExistsAtPath:path isDirectory:&isDirectory];
// 判斷下當(dāng)前文件是否存在
if (isFileExist){
unsigned long long folderSize = 0;
// 判斷下是否是文件夾
if (isDirectory) {
// 方式一:
NSEnumerator *childFilesEnum = [[mgr subpathsAtPath:path] objectEnumerator];
NSString* fileName;
while ((fileName = [childFilesEnum nextObject]) != nil){
NSString* fileAbsPath = [path stringByAppendingPathComponent:fileName];
if ([mgr fileExistsAtPath:fileAbsPath isDirectory:&isDirectory] && !isDirectory) {
folderSize += [mgr attributesOfItemAtPath:fileAbsPath error:nil].fileSize;
}
}
// // 方式二:
// NSDirectoryEnumerator *dirEnum = [mgr enumeratorAtPath:path];
// for (NSString *subpath in dirEnum) {
// NSString *fullPath = [path stringByAppendingPathComponent:subpath];
//// BOOL isDir;
// // 如果是文件夾的話就不計(jì)算大小,attributesOfItemAtPath:error:方法會計(jì)算文件夾的大小
// if ([mgr fileExistsAtPath:fullPath isDirectory:&isDirectory] && !isDirectory) {
// folderSize += [mgr attributesOfItemAtPath:fullPath error:nil].fileSize;
// }
// }
}else{
// 當(dāng)前傳入是文件
folderSize = [mgr attributesOfItemAtPath:path error:nil].fileSize;
}
// 計(jì)算完畢 -> 把計(jì)算的值傳遞出去
dispatch_sync(dispatch_get_main_queue(), ^{
if (completion) {
completion(folderSize);
}
});
}
});
}
+ (void)removeCacheWithCompletion:(void (^)())completion
{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// 創(chuàng)建文件管理者
NSFileManager *mgr = [NSFileManager defaultManager];
// 刪除文件
NSString *path = self.cachePath;
BOOL isDirectory;
BOOL isFileExist = [mgr fileExistsAtPath:path isDirectory:&isDirectory];
if (!isFileExist) return;
if (isDirectory) {
NSArray *subPaths = [mgr subpathsAtPath:path];
for (NSString *subPath in subPaths) {
NSString *filePath = [path stringByAppendingPathComponent:subPath];
[mgr removeItemAtPath:filePath error:nil];
}
}
dispatch_sync(dispatch_get_main_queue(), ^{
if (completion) {
completion();
}
});
});
}
- (void)removeCacheWithCompletion:(void (^)())completion
{
[NSObject removeCacheWithCompletion:completion];
}
@end