iOS 清除緩存

1.點(diǎn)擊單元格彈出提示


NSString *str = [self countCacheSize];

NSString *casheStr = [NSString stringWithFormat:@"緩存大小為%@,確認(rèn)清理嗎?",str];

UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"清理緩存" message:casheStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];

[alertView show];

countCacheSize

- (NSString *)countCacheSize{

long long size = 0;

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];

NSFileManager *fileMamager = [NSFileManager defaultManager];

//獲取文件夾下的子路徑

NSArray *pathArr = [fileMamager subpathsAtPath:path];

//拼接路徑

for (NSString *subPath in pathArr) {

NSString *allPath = [path stringByAppendingPathComponent:subPath];

//計(jì)算文件的大小

NSDictionary *fileDic = [fileMamager attributesOfItemAtPath:allPath error:nil];

size += [fileDic fileSize];

}

NSString *cache = [NSString stringWithFormat:@"%.2fM",size/1000.0/1000.0];

return cache;

}

2.代理

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

if(buttonIndex ==1){

NSString *filepath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];

NSFileManager *fileManger = [NSFileManager defaultManager];

NSArray *arr = [fileManger subpathsAtPath:filepath];

for (NSString *subPath in arr) {

NSString *pathStr = [filepath stringByAppendingPathComponent:subPath];

[fileManger removeItemAtPath:pathStr error:nil];

//重新創(chuàng)建cache文件夾

[fileManger createDirectoryAtPath:filepath withIntermediateDirectories:YES attributes:nil error:nil];

[_tableView reloadRowsAtIndexPaths: @[[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];

}

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容