iOS 清除缓存功能详细代码 可过滤特不删除特殊格式文件
来源:互联网 发布:数据挖掘 韩家炜 pdf 编辑:程序博客网 时间:2024/06/01 10:33
//调用删除的方法
[self folderSizeWithPath:[self getPath]];
#pragma mark - 第一步,计算缓存文件的大小
//首先获取缓存文件的路径
-(NSString *)getPath
{
//沙盒目录下library文件夹下的cache文件夹就是缓存文件夹
NSString * path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
return path;
}
-(CGFloat)folderSizeWithPath:(NSString *)path
{
//初始化文件管理类
NSFileManager * fileManager = [NSFileManager defaultManager];
float folderSize = 0.0;
if ([fileManager fileExistsAtPath:path])
{
//如果存在
//计算文件的大小
NSArray * fileArray = [fileManager subpathsAtPath:path];
for (NSString * fileName in fileArray)
{
//获取每个文件的路径
NSString * filePath = [path stringByAppendingPathComponent:fileName];
//计算每个子文件的大小
long fileSize = [fileManager attributesOfItemAtPath:filePath error:nil].fileSize;//字节数
folderSize = folderSize + fileSize / 1024.0 / 1024.0;
}
//删除缓存文件
[self deleteFileSize:folderSize];
return folderSize;
}
return 0;
}
#pragma mark -弹出是否删除的一个提示框,并且告诉用户目前有多少缓存
-(void)deleteFileSize:(CGFloat)folderSize
{
if (folderSize > 0.01)
{
UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"缓存大小:%.2fM,是否清除?",folderSize] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertView show];
}
else
{
UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"缓存已全部清理" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertView show];
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
//彻底删除文件
[self clearCacheWith:[self getPath]];
}
}
-(void)clearCacheWith:(NSString *)path
{
NSFileManager * fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:path])
{
NSArray * fileArray = [fileManager subpathsAtPath:path];
for (NSString * fileName in fileArray)
{
//可以过滤掉特殊格式的文件
if ([fileName hasSuffix:@".png"])//保留.png文件不删除
{
NSLog(@"不删除");
}
else
{
//获取每个子文件的路径
NSString * filePath = [path stringByAppendingPathComponent:fileName];
//移除指定路径下的文件
[fileManager removeItemAtPath:filePath error:nil];
}
}
}
}
最后UIalertviewdelegate记得加上,当然iOS9 都是
UIAlertController 了 大家看下我文章里的那个UIAlertController的使用方法的照着写就好了 帮得到的请点赞
- iOS 清除缓存功能详细代码 可过滤特不删除特殊格式文件
- iOS开发清除缓存功能 - 芒果iOS
- iOS开发 - Swift实现清除缓存功能
- iOS 设置中清除缓存功能
- ios简单的清除缓存代码
- 【代码笔记】iOS-清除图片缓存UIActionSheet
- iOS计算缓存文件大小, 和清除缓存功能
- 清除缓存功能(删除沙盒Libray/Caches目录)
- android 清除缓存功能
- 清除缓存功能
- 清除缓存功能
- 清除缓存功能
- 清除缓存功能
- iOS--调用第三方SDWebImage 来做清除缓存功能
- iOS开发之清除缓存功能的实现
- iOS中设置清除缓存功能的实现方法
- ios 实现艾特功能
- php 清除缓存代码
- Deep Learning Book 第十章--序列模型:循环和递归网络
- C++实验5—数组分离
- JAVA解析XML配置文件的读取操作
- android使用WebView来打开文件选择器
- 【JAVA】排序算法总览
- iOS 清除缓存功能详细代码 可过滤特不删除特殊格式文件
- 软键盘问题
- UserControl 中TagPrefix与TagName
- gdb调试
- 如何禁止Gridview上下滑动
- Mysql安装
- 接口与显式实现接口
- XA,两阶段提交和X/Open协议
- VB修改注册表