// 获取磁盘大小
//获取硬盘大小 + (CGFloat)getDiskSize { NSString *libPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSLog(@"%@", libPath); // 初始化文件管理者 NSFileManager *manager = [NSFileManager defaultManager]; // 获取lib下所有路径 NSArray *libAllPath = [manager subpathsAtPath:libPath]; NSInteger size = 0; for (NSString *path in libAllPath) { if (![path containsString:@"Preferences"]) { //把路径拼接全 NSString *pathA = [libPath stringByAppendingPathComponent:path]; //获取文件的所有信息 NSDictionary *fileAttri = [manager attributesOfItemAtPath:pathA error:nil]; //获取文件大小 size += [fileAttri[@"NSFileSize"] integerValue]; } } return size/1024.0/1024.0; }
清除缓存的方法
//清除缓存 + (void)clearDisk { NSString *libPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0]; //初始化文件管理者 NSFileManager *mgr = [NSFileManager defaultManager]; //获取lib下所有路径 NSArray *libAllPath = [mgr subpathsAtPath:libPath]; for (NSString *path in libAllPath) { if (![path containsString:@"Preferences"]) { //把路径拼接全 NSString *pathA = [libPath stringByAppendingPathComponent:path]; //移除文件 [mgr removeItemAtPath:pathA error:nil]; } } }
调用时 在清除缓存的cell中显示垃圾大小
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; } switch (indexPath.row) { case 0: cell.textLabel.text = @"活动收藏"; break; case 1: cell.textLabel.text = @"电影收藏"; break; case 2: { cell.textLabel.text = @"清除缓存"; NSString *size = [NSString stringWithFormat:@"%.2fM",[ClearDiskManager getDiskSize]]; cell.detailTextLabel.text = size; break; } default: break; } cell.backgroundColor = [UIColor cyanColor]; return cell; }
// 点击cell时清理缓存
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { switch (indexPath.row) { case 1: { ListTableViewController *listVC = [[ListTableViewController alloc] init]; [self.navigationController pushViewController:listVC animated:YES]; break; } case 0: { ActivityTableViewController *activityVC = [[ActivityTableViewController alloc] init]; [self.navigationController pushViewController:activityVC animated:YES]; break; } case 2: { UIAlertController *alerController = [UIAlertController alertControllerWithTitle:@"提示" message:@"清除缓存之后可能耗费一点你的流量" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self dismissViewControllerAnimated:YES completion:nil]; }]; UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [ClearDiskManager clearDisk]; [self.tableView reloadData]; }]; [alerController addAction:action]; [alerController addAction:action1]; [self presentViewController:alerController animated:YES completion:nil]; } default: break; } }
时间: 2024-12-19 07:36:43