IOS 清除APP缓存文件

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectoryNSUserDomainMaskYES);

NSString *cachesDir = [paths objectAtIndex:0];

[self folderSizeAtPath:cachesDir];

UILabel*cacheLab = [[UILabelalloc]initWithFrame:CGRectMake(0,10,60,20)];

cacheLab.textColor = [UIColorcolorWithHexString:@"#404040"];

cacheLab.numberOfLines=0;

cacheLab.textAlignment=NSTextAlignmentRight;

cacheLab.font = [UIFontsystemFontOfSize:14];

cacheLab.text=[NSStringstringWithFormat:@"%.2fM",[selffolderSizeAtPath:cachesDir]];

[self addSubview:cacheLab];

UITapGestureRecognizer* cache = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(cacheTap)];

[cacheLabaddGestureRecognizer:cache];


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

    NSFileManager* manager = [NSFileManagerdefaultManager];

    if (![managerfileExistsAtPath:folderPath])return0;

    NSEnumerator *childFilesEnumerator = [[managersubpathsAtPath:folderPath]objectEnumerator];//从前向后枚举器//////

    NSString* fileName;

    long long folderSize = 0;

    while ((fileName = [childFilesEnumeratornextObject]) !=nil){

        //NSLog(@"fileName ==== %@",fileName);

        NSString* fileAbsolutePath = [folderPathstringByAppendingPathComponent:fileName];

        //NSLog(@"fileAbsolutePath ==== %@",fileAbsolutePath);

        folderSize += [selffileSizeAtPath:fileAbsolutePath];

    }

    //NSLog(@"folderSize ==== %lld",folderSize);

    return folderSize/(1024.0*1024.0);

}


//计算缓存文件的大小的M

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

    NSFileManager* manager = [NSFileManagerdefaultManager];

    if ([managerfileExistsAtPath:filePath]){

        return [[managerattributesOfItemAtPath:filePatherror:nil]fileSize];

    }

    

    return0;

}


-(void)cacheTap{

    UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"温馨提示"

                                                    message:@"是否确定清除缓存?"

                                                   delegate:self

                                          cancelButtonTitle:@"取消"

                                          otherButtonTitles:@"确定",nil];

    alert.delegate=self;

    

    [alert show];

}


//根据被点击按钮的索引处理点击事件

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    if (buttonIndex==0) {

        

    }elseif (buttonIndex==1){

        NSArray *paths =NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);

        NSString *cachesDir = [pathsobjectAtIndex:0];

        

        NSArray *files = [[NSFileManagerdefaultManager]subpathsAtPath:cachesDir];

        for (NSString *pin files) {

            NSError *error;

            NSString *path = [cachesDirstringByAppendingPathComponent:p];

            if ([[NSFileManagerdefaultManager]fileExistsAtPath:path]) {

                [[NSFileManagerdefaultManager]removeItemAtPath:patherror:&error];

            }

        }

        [selfperformSelectorOnMainThread:@selector(clearCacheSuccess)withObject:nilwaitUntilDone:YES];

        

    }

    

}


-(void)clearCacheSuccess

{

    cacheLab.text=@"0M";

    [HUDProgressshowInView:self.navigationController.viewwithTitle:@"清理缓存成功"type:HUDProgressTypeError];

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值