美文网首页
iOS清除缓存

iOS清除缓存

作者: 破夕_____________ | 来源:发表于2016-01-06 15:44 被阅读290次

#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:nil 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"])

{

NSLog(@"不删除");

}

else

{

//获取每个子文件的路径

NSString * filePath = [path stringByAppendingPathComponent:fileName];

//移除指定路径下的文件

[fileManager removeItemAtPath:filePath error:nil];

}

}

}

}

相关文章

  • WkWebView 清除缓存 ios8

    WkWebView 清除缓存 ios8iOS8系统NSString *libraryDir = NSSearchP...

  • 清楚WKWebView缓存

    WKWebView清除缓存WKWebView,在iOS9以后提供了缓存管理类WKWebsiteDataStore,...

  • iOS清除UIWebView缓存

    使用iOS的webview会自动进行缓存,在开发的时候要记得清除Cookie和缓存。

  • iOS 清除缓存

    iOS的缓存一般分为两部分,一部分是下载数据产生的缓存,这部分有系统做了缓存,在沙盒里面,还有一部分是图片的缓存,...

  • iOS清除缓存

  • 清除缓存ios

  • iOS清除缓存

    #pragma mark - 第一步,计算缓存文件的大小 //首先获取缓存文件的路径 -(NSString *)g...

  • ios清除缓存

  • iOS 清除缓存

    iOS 清除缓存 我们在使用任何一款APP的时候,无论是苹果的 还是安卓的 都会产生一些缓存 ,我们在使用APP的...

  • iOS 清除缓存

    我们在使用任何一款APP的时候,无论是苹果的 还是安卓的 都会产生一些缓存 ,我们在使用APP的时候 就要定期去清...

网友评论

      本文标题:iOS清除缓存

      本文链接:https://www.haomeiwen.com/subject/wovxkttx.html