iOS开发 - 清除缓存

//缓存

//  WBClearCacheTool.mdebug

//  buybuyART调试

//orm

//  Created by wangbin on 2018/1/26.rem

//  Copyright © 2018年 ArtAlly Information Technology Co., Ltd. All rights reserved.get

//string

 

#import "WBClearCacheTool.h"it

#define fileManager [NSFileManager defaultManager]io

@implementation WBClearCacheToolform

//获取path路径下文件夹大小

+ (NSString *)getCacheSizeWithFilePath:(NSString *)path

{

//调试

#ifdef DEBUG

    //若是文件夹不存在或者不是一个文件夹那么就抛出一个异常

    //抛出异常会致使程序闪退,因此只在调试阶段抛出,发布阶段不要再抛了,否则极度影响用户体验

    BOOL isDirectory = NO;

    BOOL isExist = [fileManager fileExistsAtPath:path isDirectory:&isDirectory];

    if (!isExist || !isDirectory)

    {

        NSException *exception = [NSException exceptionWithName:@"fileError" reason:@"please check your filePath!" userInfo:nil];

        [exception raise];

    }

    NSLog(@"debug");

//发布

#else

    NSLog(@"release");

#endif

    //获取“path”文件夹下面的全部文件

    NSArray *subpathArray= [fileManager subpathsAtPath:path];

    NSString *filePath = nil;

    NSInteger totleSize=0;

    for (NSString *subpath in subpathArray)

    {

        //拼接每个文件的全路径

        filePath =[path stringByAppendingPathComponent:subpath];

        //isDirectory,是不是文件夹,默认不是

        BOOL isDirectory = NO;

        //isExist,判断文件是否存在

        BOOL isExist = [fileManager fileExistsAtPath:filePath isDirectory:&isDirectory];

        //判断文件是否存在,不存在的话过滤

        //若是存在的话,那么是不是文件夹,是的话也过滤

        //若是文件既存在又不是文件夹,那么判断它是否是隐藏文件,是的话也过滤

        //过滤以上三个状况后,就是一个文件夹里面真实的文件的总大小

        //以上判断目的是忽略不须要计算的文件

        if (!isExist || isDirectory || [filePath containsString:@".DS"]) continue;

            //NSLog(@"%@",filePath);

            //指定路径,获取这个路径的属性

            //attributesOfItemAtPath:须要传文件夹路径

            //可是attributesOfItemAtPath 只能够得到文件属性,不能够得到文件夹属性,这个也就是须要for-in遍历文件夹里面每个文件的缘由

            NSDictionary *dict=   [fileManager attributesOfItemAtPath:filePath error:nil];

            NSInteger size=[dict[@"NSFileSize"] integerValue];

            totleSize+=size;

     }

    //将文件夹大小转换为 M/KB/B

    NSString *totleStr = nil;

    if (totleSize > 1000 * 1000)

    {

      totleStr = [NSString stringWithFormat:@"%.1fM",totleSize / 1000.0f /1000.0f];

    }else if (totleSize > 1000)

    {

      totleStr = [NSString stringWithFormat:@"%.1fKB",totleSize / 1000.0f ];

    }else

    {

      totleStr = [NSString stringWithFormat:@"%.1fB",totleSize / 1.0f];

    }

    return totleStr;

}

 

//清除path文件夹下缓存大小

+ (BOOL)clearCacheWithFilePath:(NSString *)path

{

    //拿到path路径的下一级目录的子文件夹

    NSArray *subpathArray = [fileManager contentsOfDirectoryAtPath:path error:nil];

    NSError *error;

    NSString *filePath;

    for (NSString *subpath in subpathArray)

    {

        if (![subpath isEqualToString:@"Snapshots"])//这个文件无访问权限

        {

            filePath =[path stringByAppendingPathComponent:subpath];

            //删除子文件夹

            [fileManager removeItemAtPath:filePath error:&error];

            if (error) {

                NSLog(@"%@",[NSString stringWithFormat:@"清理文件夹失败路径:%@",filePath]);

                [MyHUD showInView:ShareApp.window text:@"清理缓存失败"];

                return NO;

            }else{

                NSLog(@"%@",[NSString stringWithFormat:@"清除文件夹成功路径:%@",filePath]);

            }

        }

       

    }

    [MyHUD showInView:ShareApp.window text:@"清理缓存成功"];

    NSLog(@"清理缓存成功");

//    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//手机震动

    return YES;

}

@end

相关文章
相关标签/搜索