iOS:获取APP缓存与清除缓存

1.清除APP缓存:

/*
 自定义方法,清除APP缓存
 */
- (void)clearCache{
    //获取文件管理器
    NSFileManager *fileM = [NSFileManager defaultManager];
    //获取缓存路径
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    //清除缓存
    [fileM removeItemAtPath:cachePath error:nil];
}

2.获取APP缓存大小:

/*
 自定义方法:获取缓存大小
 */
- (long long)getCacheSize{
    //获取文件管理器
    NSFileManager *fileM = [NSFileManager defaultManager];
    //获取缓存路径
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    
    //得到缓存文件列表
    NSArray *childPaths = [fileM subpathsAtPath:cachePath];
    
    //存储文件总长度
    long long size = 0;
    for(NSString *childPath in childPaths){
        //得到文件路径
        NSString *filePath = [cachePath stringByAppendingPathComponent:childPath];
        
        //获取当前是文件还是目录
        BOOL isDir = NO;
        [fileM fileExistsAtPath:filePath isDirectory:&isDir];

        if(isDir){//如果是目录则跳出此次循环
            continue;
        }
        
        //将文件大小累加
        size = size + [[fileM attributesOfItemAtPath:filePath error:nil][NSFileSize] longLongValue];
    }
    return size;
}

 

你可能感兴趣的:(iOS)