IOS計算清理緩存

1.計算APP緩存

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

        NSString *cachesDir = [paths objectAtIndex:0];

        NSString *filePath = [cachesDir stringByAppendingPathComponent:@"com.XXXXX.HappyShopping"];

        NSFileManager * manager=[NSFileManager defaultManager];

        cell.cacheLabel.text =[NSString stringWithFormat:@"%0.2fM",[[manager attributesOfItemAtPath:filePath error:nil] fileSize]/1024.f] ;

2.清除緩存

[SVProgressHUD showWithStatus:@"正在清除緩存" maskType:SVProgressHUDMaskTypeBlack];

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

     NSString *cachesDir = [path objectAtIndex:0];

    NSFileManager *fileManager=[NSFileManager defaultManager];

    if ([fileManager fileExistsAtPath:cachesDir]) {

        NSArray *childerFiles=[fileManager subpathsAtPath:cachesDir];

        NSLog(@"%@",childerFiles);

        for (NSString *fileName in childerFiles)

        {

            //如有需要,加入條件,過濾掉不想刪除的文件

            NSString *absolutePath=[cachesDir stringByAppendingPathComponent:fileName];

            [fileManager removeItemAtPath:absolutePath error:nil];

        }

    }


    [SVProgressHUD showSuccessWithStatus:@"成功清除緩存"];

    [self.myTabelView reloadData];


發佈了73 篇原創文章 · 獲贊 4 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章