这是我做的清除缓存功能,还是很好用的
//
// 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