【源码】iOS开发 - 清除缓存

//

//  WBClearCacheTool.m

//  buybuyART

//

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

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

//

 

#import "WBClearCacheTool.h"

#define fileManager [NSFileManager defaultManager]

@implementation WBClearCacheTool

//获取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

 

转载于:https://www.cnblogs.com/wangbinios/p/8431782.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值