简单的清除缓存功能

向大家介绍下,自己写得简单地清除缓存功能,可能会有些用,可以帮助大家, 那样我是最开心的.也可能还有很多缺陷问题,希望大家给些建议.让我改正,我同样开心

首先创建一个UIAlertController,来显示缓存大小,还有是否清除

简单的清除缓存功能_第1张图片
8F65C9B2-172D-4E63-BEE4-1753602F71ED.png
// 创建UIAlertController
- (void)createAlertController
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示信息" message:@"是否清除缓存" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self ClearCache];
    }];
    [alertController addAction:actionCancel];
    [alertController addAction:actionOk];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

下面就是计算缓存的内容

// 计算清除的缓存

- (void)CalculateCache
{
    //找到缓存路径
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *path = [paths lastObject];
   //folderSizeAtPath这个方法是遍历文件夹获得文件夹大小,下面封装的.
    float size = [self folderSizeAtPath:path];
    self.strOfCache = [NSString stringWithFormat:@"缓存:%.1fM", size];
   
    [self.tableView reloadData];
}

// 遍历文件夹获得文件夹大小, 返回多少m
- (float) folderSizeAtPath:(NSString *)folderPath
{
    //创建文件管理对象,
    NSFileManager *manager = [NSFileManager defaultManager];
    // 判断路径是否存在
    if (![manager fileExistsAtPath:folderPath])
    {
        return 0;
    }
    else
    {
        NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
        NSString *fileName;
        // 用来累加缓存的folderSize
        long long folderSize = 0;
        while ((fileName = [childFilesEnumerator nextObject]) != nil)
        {
            NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
            //fileSizeAtPath方法是计算单个文件的大小, 下面封装
            folderSize += [self fileSizeAtPath:fileAbsolutePath];
        }
        
        return folderSize / (1024.0 * 1024.0);
    }
}

// 计算单个文件的大小
- (long long)fileSizeAtPath:(NSString *)filePath
{
   
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]) {
        
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];

    }else{
        return 0;
    }
}
// 清除缓存
- (void)ClearCache
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *path = [paths lastObject];
    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:path];
    //遍历清除每个文件缓存
    for (NSString *p in files)
    {
        NSError *error;
        NSString *Path = [path stringByAppendingPathComponent:p];
        if ([[NSFileManager defaultManager] fileExistsAtPath:Path])
        {
            [[NSFileManager defaultManager] removeItemAtPath:Path error:&error];
        }
    }
    //重写计算缓存
    [self CalculateCache];
}

你可能感兴趣的:(简单的清除缓存功能)