Commit 10ab82db cgx

完成清除缓存功能

1 个父辈 fe09f1ce
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
39CC172FB610DA5F29120B56 /* Pods_DreamSleep.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2D11699D88A7A7BDB6168B71 /* Pods_DreamSleep.framework */; settings = {ATTRIBUTES = (Required, ); }; }; 39CC172FB610DA5F29120B56 /* Pods_DreamSleep.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2D11699D88A7A7BDB6168B71 /* Pods_DreamSleep.framework */; settings = {ATTRIBUTES = (Required, ); }; };
D00291892816CD4500C0573B /* DsCacheUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = D00291882816CD4500C0573B /* DsCacheUtils.m */; };
D01814D127FFCBAF00583D4E /* CWCarousel.m in Sources */ = {isa = PBXBuildFile; fileRef = D01814CB27FFCBAF00583D4E /* CWCarousel.m */; }; D01814D127FFCBAF00583D4E /* CWCarousel.m in Sources */ = {isa = PBXBuildFile; fileRef = D01814CB27FFCBAF00583D4E /* CWCarousel.m */; };
D01814D227FFCBAF00583D4E /* CWFlowLayout.m in Sources */ = {isa = PBXBuildFile; fileRef = D01814CD27FFCBAF00583D4E /* CWFlowLayout.m */; }; D01814D227FFCBAF00583D4E /* CWFlowLayout.m in Sources */ = {isa = PBXBuildFile; fileRef = D01814CD27FFCBAF00583D4E /* CWFlowLayout.m */; };
D01814D527FFCCFA00583D4E /* HomeTableView.m in Sources */ = {isa = PBXBuildFile; fileRef = D01814D427FFCCFA00583D4E /* HomeTableView.m */; }; D01814D527FFCCFA00583D4E /* HomeTableView.m in Sources */ = {isa = PBXBuildFile; fileRef = D01814D427FFCCFA00583D4E /* HomeTableView.m */; };
...@@ -126,6 +127,8 @@ ...@@ -126,6 +127,8 @@
2D11699D88A7A7BDB6168B71 /* Pods_DreamSleep.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_DreamSleep.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 2D11699D88A7A7BDB6168B71 /* Pods_DreamSleep.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_DreamSleep.framework; sourceTree = BUILT_PRODUCTS_DIR; };
72E2054B343085AE9898C560 /* Pods-DreamSleep.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DreamSleep.debug.xcconfig"; path = "Target Support Files/Pods-DreamSleep/Pods-DreamSleep.debug.xcconfig"; sourceTree = "<group>"; }; 72E2054B343085AE9898C560 /* Pods-DreamSleep.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DreamSleep.debug.xcconfig"; path = "Target Support Files/Pods-DreamSleep/Pods-DreamSleep.debug.xcconfig"; sourceTree = "<group>"; };
9B4D4A8254C6E2872914AB4D /* Pods-DreamSleep.beta.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DreamSleep.beta.xcconfig"; path = "Target Support Files/Pods-DreamSleep/Pods-DreamSleep.beta.xcconfig"; sourceTree = "<group>"; }; 9B4D4A8254C6E2872914AB4D /* Pods-DreamSleep.beta.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DreamSleep.beta.xcconfig"; path = "Target Support Files/Pods-DreamSleep/Pods-DreamSleep.beta.xcconfig"; sourceTree = "<group>"; };
D00291872816CD4500C0573B /* DsCacheUtils.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DsCacheUtils.h; sourceTree = "<group>"; };
D00291882816CD4500C0573B /* DsCacheUtils.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DsCacheUtils.m; sourceTree = "<group>"; };
D01814CB27FFCBAF00583D4E /* CWCarousel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CWCarousel.m; sourceTree = "<group>"; }; D01814CB27FFCBAF00583D4E /* CWCarousel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CWCarousel.m; sourceTree = "<group>"; };
D01814CC27FFCBAF00583D4E /* CWCarouselHeader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CWCarouselHeader.h; sourceTree = "<group>"; }; D01814CC27FFCBAF00583D4E /* CWCarouselHeader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CWCarouselHeader.h; sourceTree = "<group>"; };
D01814CD27FFCBAF00583D4E /* CWFlowLayout.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CWFlowLayout.m; sourceTree = "<group>"; }; D01814CD27FFCBAF00583D4E /* CWFlowLayout.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CWFlowLayout.m; sourceTree = "<group>"; };
...@@ -421,6 +424,8 @@ ...@@ -421,6 +424,8 @@
D070509928071BAF006D72E1 /* DSNetworkTool.m */, D070509928071BAF006D72E1 /* DSNetworkTool.m */,
D0E65FFD2807AC5E006562F2 /* DSProgressHUD.h */, D0E65FFD2807AC5E006562F2 /* DSProgressHUD.h */,
D0E65FFE2807AC5E006562F2 /* DSProgressHUD.m */, D0E65FFE2807AC5E006562F2 /* DSProgressHUD.m */,
D00291872816CD4500C0573B /* DsCacheUtils.h */,
D00291882816CD4500C0573B /* DsCacheUtils.m */,
); );
path = Network; path = Network;
sourceTree = "<group>"; sourceTree = "<group>";
...@@ -1086,6 +1091,7 @@ ...@@ -1086,6 +1091,7 @@
D09D0E9A280D507F008DEDAB /* ProfileAlertView.m in Sources */, D09D0E9A280D507F008DEDAB /* ProfileAlertView.m in Sources */,
D0506B0C280503A800229278 /* UIButton+Extras.m in Sources */, D0506B0C280503A800229278 /* UIButton+Extras.m in Sources */,
D0C50B3F27FD381000DC68F0 /* UIView+Extras.m in Sources */, D0C50B3F27FD381000DC68F0 /* UIView+Extras.m in Sources */,
D00291892816CD4500C0573B /* DsCacheUtils.m in Sources */,
D07DAC902810546A0067A1BF /* DSImagePickerController.m in Sources */, D07DAC902810546A0067A1BF /* DSImagePickerController.m in Sources */,
D0DF90AE2814390000FC0F64 /* BannerModel.m in Sources */, D0DF90AE2814390000FC0F64 /* BannerModel.m in Sources */,
D0E65FFF2807AC5E006562F2 /* DSProgressHUD.m in Sources */, D0E65FFF2807AC5E006562F2 /* DSProgressHUD.m in Sources */,
......
//
// DsCacheUtils.h
// DreamSleep
//
// Created by peter on 2022/4/25.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
/// 读取缓存、清理缓存工具
@interface DsCacheUtils : NSObject
/// 获取缓存大小(以M为单位)
+ (NSString *)getCacheSize;
/// 清除缓存
/// @param successBlock successBlock
+ (void)cleanCacheWithSuccess:(void (^)(BOOL success))successBlock;
@end
NS_ASSUME_NONNULL_END
//
// DsCacheUtils.m
// DreamSleep
//
// Created by peter on 2022/4/25.
//
#import "DsCacheUtils.h"
@implementation DsCacheUtils
+ (NSString *)getCacheSize {
// 得到缓存路径
NSString * path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
NSFileManager * manager = [NSFileManager defaultManager];
CGFloat size = 0;
// 首先判断是否存在缓存文件
if ([manager fileExistsAtPath:path]) {
NSArray * childFile = [manager subpathsAtPath:path];
for (NSString * fileName in childFile) {
// 缓存文件绝对路径
NSString * absolutPath = [path stringByAppendingPathComponent:fileName];
size = size + [manager attributesOfItemAtPath:absolutPath error:nil].fileSize;
}
// 计算SDWebimage5的缓存和系统缓存总和
// size = size + [[SDImageCache sharedImageCache] totalDiskSize];
}
YYImageCache *cache = [YYWebImageManager sharedManager].cache;
NSInteger discCache = cache.diskCache.totalCost;
NSInteger memoryCache = cache.memoryCache.totalCost;
size = size + (discCache + memoryCache);
if (size < 0) { return @"0.0M"; }
double returnSize = size / 1024.0 / 1024.0;
return [NSString stringWithFormat:@"%.1fM", returnSize];
}
+ (void)cleanCacheWithSuccess:(void (^)(BOOL success))successBlock {
// 获取缓存路径
NSString * path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
NSFileManager * manager = [NSFileManager defaultManager];
// 判断是否存在缓存文件
if ([manager fileExistsAtPath:path]) {
NSArray * childFile = [manager subpathsAtPath:path];
// 逐个删除缓存文件
for (NSString *fileName in childFile) {
NSString * absolutPat = [path stringByAppendingPathComponent:fileName];
[manager removeItemAtPath:absolutPat error:nil];
}
// 清除yywebimage缓存
YYImageCache *cache = [YYWebImageManager sharedManager].cache;
[cache.memoryCache removeAllObjects];
[cache.diskCache removeAllObjects];
// 删除sdwebimage的缓存
// [[SDWebImageManager sharedManager].imageCache clearWithCacheType:SDImageCacheTypeAll completion:^{
// // 这里是又调用了得到缓存文件大小的方法,是因为不确定是否删除了所有的缓存,所以要计算一遍,展示出来
// if ([[self getCacheSize] isEqualToString:@"0.00M"]) {
// if (successBlock) {
// successBlock(YES);
// }
// [DSProgressHUD showToast:@"清除缓存成功"];
// } else {
// if (successBlock) {
// successBlock(NO);
// }
// [DSProgressHUD showToast:@"清除缓存失败"];
// }
// }];
}
}
@end
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
// //
#import "SetTableView.h" #import "SetTableView.h"
#import "DsCacheUtils.h"
/// 设置自定义数据模型 /// 设置自定义数据模型
@interface SetModel : NSObject @interface SetModel : NSObject
...@@ -18,7 +19,8 @@ ...@@ -18,7 +19,8 @@
+ (NSArray *)getAllSetDatas { + (NSArray *)getAllSetDatas {
NSArray *titles = @[@"应用版本及升级", @"用户协议", @"隐私政策", @"清除缓存"]; NSArray *titles = @[@"应用版本及升级", @"用户协议", @"隐私政策", @"清除缓存"];
NSString *version = [NSString stringWithFormat:@"当前版本%@", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]]; NSString *version = [NSString stringWithFormat:@"当前版本%@", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]];
NSArray *details = @[version, @"", @"", @"10.0M"]; NSString *cacheSize = [DsCacheUtils getCacheSize];
NSArray *details = @[version, @"", @"", cacheSize];
NSMutableArray *tmpArr = [NSMutableArray array]; NSMutableArray *tmpArr = [NSMutableArray array];
for (int i = 0; i < titles.count; i++) { for (int i = 0; i < titles.count; i++) {
SetModel *m = [SetModel new]; SetModel *m = [SetModel new];
...@@ -45,7 +47,7 @@ ...@@ -45,7 +47,7 @@
self.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, CornerViewDarkColor, DSWhite); self.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, CornerViewDarkColor, DSWhite);
self.rightIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"moreIcon"]]; self.rightIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"moreIcon"]];
[self.contentView addSubview:self.rightIcon]; [self.contentView addSubview:self.rightIcon];
self.titleLab = [UILabel dkLabWithFont:SysFont(15.0)]; self.titleLab = [UILabel dkLabWithFont:SysFont(15.0)];
[self.contentView addSubview:self.titleLab]; [self.contentView addSubview:self.titleLab];
...@@ -242,9 +244,11 @@ ...@@ -242,9 +244,11 @@
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil]; [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
} }
} else if (indexPath.row == 3) { // 清除缓存 } else if (indexPath.row == 3) { // 清除缓存
[DsCacheUtils cleanCacheWithSuccess:^(BOOL success) {
}];
[DSProgressHUD showToast:@"清除成功"]; [DSProgressHUD showToast:@"清除成功"];
SetModel *m = self.dataArr[indexPath.row]; SetModel *m = self.dataArr[indexPath.row];
m.detail = @"0M"; m.detail = @"0.0M";
[tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationNone]; [tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
} else { } else {
if (self.setDelegate && [self.setDelegate respondsToSelector:@selector(didSelectRowAtIndexPath:)]) { if (self.setDelegate && [self.setDelegate respondsToSelector:@selector(didSelectRowAtIndexPath:)]) {
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!