//获取所有文件的数组
NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachePath];
for(NSString *path in files) {
NSString*filePath = [cachePath stringByAppendingString:[NSString stringWithFormat:@"/%@",path]];
//累加
folderSize += [[NSFileManager defaultManager]attributesOfItemAtPath:filePath error:nil].fileSize;
}
//获取缓存路径
NSString*cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)objectAtIndex:0];
//返回路径中的文件数组
NSArray*files = [[NSFileManager defaultManager]subpathsAtPath:cachePath];
// NSLog(@"文件数:%ld",[files count]);
for(NSString *p in files){
NSError*error;
NSString*path = [cachePath stringByAppendingString:[NSString stringWithFormat:@"/%@",p]];
if([[NSFileManager defaultManager]fileExistsAtPath:path])
{
BOOL isRemove = [[NSFileManager defaultManager]removeItemAtPath:path error:&error];
if(isRemove) {
NSLog(@"清除成功");
//这里发送一个通知给外界,外界接收通知,可以做一些操作(比如UIAlertViewController)
self.cacheLabel.text = @"0.00M";
// CGFloat MemorySize = [self folderSize] + [self cachesSize];
self.memoryLabel.text = [NSString stringWithFormat:@"%.2fM",[self cachesSize]];
}else{
NSLog(@"清除失败");
}
}
}
网友评论