iOS清除缓存,实时显示缓存大小


这是我做的清除缓存功能,还是很好用的


//

//  SettingViewController.m

//  GuiguziFoot

//

//  Created by LANJIE on 16/5/20.

//  Copyright © 2016 俞涛涛. All rights reserved.

//


#import "SettingViewController.h"

#import "MBProgressHUD.h"

 


@interface SettingViewController ()

@property (weak, nonatomic) IBOutlet UILabel *cacheLabel;


@end


@implementation SettingViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    _cacheLabel.text=[NSString stringWithFormat:@"%.1fM",[self filePath]];//显示缓存大小

    // Do any additional setup after loading the view from its nib.

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/

//蒙版

- (void)textExample {

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];

    

    // Set the annular determinate mode to show task progress.

    hud.mode = MBProgressHUDModeText;

    hud.label.text = NSLocalizedString(@"清除缓存成功", @"HUD message title");

    // Move to bottm center.

    hud.offset = CGPointMake(0.f, 0.f);

    

    [hud hideAnimated:YES afterDelay:2.f];

}

/**

 *  清除缓存

 *

 *  @param sender

 */

- (IBAction)cleanCache:(UIControl *)sender {

    UIAlertController *alertSheet=[UIAlertController alertControllerWithTitle:@"清除缓存" message:@"确认清除缓存" preferredStyle:UIAlertControllerStyleAlert];

    

    // 创建按钮

    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {

        //清除缓存方法

        [self clearFile];

 


    }];

 

    // 注意取消按钮只能添加一个

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction *action) {

        // 点击按钮后的方法直接在这里面写

        NSLog(@"取消·");

    }];

    [alertSheet addAction:okAction];

//    [alertSheet addAction:okAction2];

    [alertSheet addAction:cancelAction];

    [self presentViewController:alertSheet animated:YES completion:nil];

    

    

}


- (IBAction)backClick:(id)sender {

    [self.navigationController popViewControllerAnimated:YES];

}




// 显示缓存大小

-( float )filePath

{

    

    NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];

    

    return [ self folderSizeAtPath :cachPath];

    

}

//1:首先我们计算一下 单个文件的大小


- ( long long ) fileSizeAtPath:( NSString *) filePath{

    

    NSFileManager * manager = [ NSFileManager defaultManager ];

    

    if ([manager fileExistsAtPath :filePath]){

        

        return [[manager attributesOfItemAtPath :filePath error : nil ] fileSize ];

    }

    

    return 0 ;

    

}

//2:遍历文件夹获得文件夹大小,返回多少 M(提示:你可以在工程界设置()m


- ( float ) folderSizeAtPath:( NSString *) folderPath{

    

    NSFileManager * manager = [ NSFileManager defaultManager ];

    

    if (![manager fileExistsAtPath :folderPath]) return 0 ;

    

    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator ];

    

    NSString * fileName;

    

    long long folderSize = 0 ;

    

    while ((fileName = [childFilesEnumerator nextObject ]) != nil ){

        

        NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName];

        

        folderSize += [ self fileSizeAtPath :fileAbsolutePath];

        

    }

    

    return folderSize/( 1024.0 * 1024.0 );

    

}



// 清理缓存


- (void)clearFile

{

    dispatch_async(

                   dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

                   , ^{

    NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];

    

    NSArray * files = [[ NSFileManager defaultManager ] subpathsAtPath :cachPath];

    

    NSLog ( @"cachpath = %@" , cachPath);

    

    for ( NSString * p in files) {

        

        NSError * error = nil ;

        

        NSString * path = [cachPath stringByAppendingPathComponent :p];

        

        if ([[ NSFileManager defaultManager ] fileExistsAtPath :path]) {

            

            [[ NSFileManager defaultManager ] removeItemAtPath :path error :&error];

            

        }

        

    }

    [ self performSelectorOnMainThread : @selector (clearCachSuccess) withObject : nil waitUntilDone : YES ];});

    

}

-(void)clearCachSuccess

{

    NSLog ( @" 清理成功 " );

//    [self.view setNeedsDisplay];

    _cacheLabel.text=[NSString stringWithFormat:@"%.1fM",[self filePath]];

    [self textExample];


}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值