清理缓存

来源:互联网 发布:福利app软件 编辑:程序博客网 时间:2024/04/29 18:16

#pragma mark - 清理缓存

- (void)clearCache

{

    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES)firstObject];

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

    for (NSString *pin files) {

        NSError *error = nil;

        NSString *path = [cachePath stringByAppendingPathComponent:p];

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

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

        }

    }

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

}

- (void)clearSuccess

{

    [self.tableViewreloadSections:[NSIndexSetindexSetWithIndex:0]withRowAnimation:UITableViewRowAnimationNone];

    NSString *title = @"恭喜您";

    NSString *message = [NSStringstringWithFormat:@"您已成功清理%.1fM缓存",self.cacheData];

    UIAlertController *ac = [UIAlertControlleralertControllerWithTitle:title message:messagepreferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *cancel = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleCancelhandler:^(UIAlertAction *_Nonnull action) {

        NSLog(@"success");

    }];

    [ac addAction:cancel];

    [selfpresentViewController:ac animated:YEScompletion:^{

        

    }];

}

- (CGFloat)cacheData

{

    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES)firstObject];

    CGFloat cacheData = [selffolderSizeAtPath:cachePath];

    self.cacheData = cacheData;

    return cacheData;

}

- (CGFloat)folderSizeAtPath:(NSString *)folderPath

{

    NSFileManager *manager = [NSFileManagerdefaultManager];

    if (![manager fileExistsAtPath:folderPath]) {

        return 0;

    }

    NSEnumerator *childFilesEnumerator = [[managersubpathsAtPath:folderPath]objectEnumerator];

    NSString *fileName = nil;

    long long folderSize =0;

    while ((fileName = [childFilesEnumerator nextObject]) != nil) {

        NSString *fileAbsolutePath = [folderPathstringByAppendingPathComponent:fileName];

        folderSize += [self fileSizeAtPath:fileAbsolutePath];

    }

    return folderSize / (1024.0 *1024.0);

}

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

{

    NSFileManager *manager = [NSFileManagerdefaultManager];

    if ([manager fileExistsAtPath:filePath]) {

        return [[managerattributesOfItemAtPath:filePath error:nil]fileSize];

    }

    return 0;

}


0 0
原创粉丝点击