• 获得APP缓存大小并清除缓存 iOS


    读取缓存大小,目前计算了SDWebimage缓存大小,Documents目录下某个文件夹大小(类似可以继续添加)

    -(CGFloat)readCacheSize;

    清空缓存,目前清空了SDWebimage缓存,Documents目录下某个文件夹缓存(类似可以继续添加),Webview缓存

    typedef void(^clearCacheSuccessBlock)(void);
    
    -(void)clearCacheWithCompletionBlock:(nullable clearCacheSuccessBlock)completionBlock;

    代码示例

    -(CGFloat)readCacheSize
    {
        CGFloat size=0;
        //获取SDWebImage磁盘缓存大小
        CGFloat size_SDWebImage = [self readSDWebImageCacheSize];
        size = size + size_SDWebImage;
        //获取短视频背景音乐缓存大小
        CGFloat size_ShortVideoBgm = [self readShortVideoBgmCacheSize];
        size = size + size_ShortVideoBgm;
        
        CGFloat sizeMB = size>0?(size/1024.0/1024.0):0;
        return sizeMB;
    }
    
    #pragma mark - 清空缓存
    -(void)clearCacheWithCompletionBlock:(nullable clearCacheSuccessBlock)completionBlock{
        [HUDManager showLoadingHUDView:[UIViewController currentViewController].view withText:@"正在清除缓存···"];
        
        
        //清除H5缓存
        [self clearURLCacheAndHTTPCookie];
        
        //清除SDWebImage缓存
        [self clearSDWebImageCache];
        
        //清除短视频背景音乐缓存
        [self clearShortVideoBgmCache];
       
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [HUDManager  hideHUDView];
            [[UIViewController currentViewController].view showMessageWithText:@"已清空缓存"];
        });
    }
    
    //获取SDWebImage磁盘缓存大小
    -(CGFloat)readSDWebImageCacheSize
    {
        //SDImageCache
        NSLog(@"图片缓存路径%@",[SDImageCache sharedImageCache].diskCachePath);
        NSUInteger size = [[SDImageCache sharedImageCache] totalDiskSize];
        return size;
    }
    
    #pragma mark - 清空缓存
    -(void)clearSDWebImageCache{
        [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
            
        }];
        [[SDImageCache sharedImageCache] clearMemory];
    }
    
    //清除webview缓存
    - (void)clearURLCacheAndHTTPCookie
    {
        NSSet *websiteDataTypes = [NSSet setWithArray:@[
                                                         WKWebsiteDataTypeDiskCache,
                                                         WKWebsiteDataTypeOfflineWebApplicationCache,
                                                         WKWebsiteDataTypeMemoryCache,
                                                         WKWebsiteDataTypeLocalStorage,
                                                         WKWebsiteDataTypeCookies,
                                                         WKWebsiteDataTypeSessionStorage,
                                                         WKWebsiteDataTypeIndexedDBDatabases,
                                                         WKWebsiteDataTypeWebSQLDatabases
                                                         ]];
        NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
        [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
            // 结束回调
        }];
    }
    
    //获取短视频背景音乐缓存大小
    -(CGFloat)readShortVideoBgmCacheSize
    {
        float size = 0;
        NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/bgm"];
        if(![[NSFileManager defaultManager] fileExistsAtPath:path]){
            return 0;
        }
        size=(float)[self totalSizeAtPath:path];
        return size;
    }
    
    //清除短视频背景音乐缓存
    -(void)clearShortVideoBgmCache
    {
        NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/bgm"];
        [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
    }
    
    //获得当前文件夹及下面所有文件大小
    - (uint64_t)totalSizeAtPath:(NSString *)path {
        NSFileManager *fileManager = NSFileManager.defaultManager;
        NSDictionary<NSString *, id> *attributes = [fileManager attributesOfItemAtPath:path error:NULL];
        uint64_t totalSize = [attributes fileSize];
        
        for (NSString *fileName in [fileManager enumeratorAtPath:path]) {
            attributes = [fileManager attributesOfItemAtPath:[path stringByAppendingPathComponent:fileName] error:NULL];
            totalSize += [attributes fileSize];
        }
        return totalSize;
    }

    备注:

    以下代码只能获取当前文件夹大小,不能获得子目录文件夹大小

    [attributes fileSize];
  • 相关阅读:
    Redis学习-发布/订阅
    Redis学习-Sentinel
    Redis学习-复制
    Redis学习-持久化
    Redis学习-Set
    Redis学习-SortedSet
    mac下使用apktool反编译
    ImageView setImageURI图片不改变NetWorkImageView 不显示的问题
    使用SharedPreference和对象流存储对象
    解决百度云推送通知,不显示默认Notification
  • 原文地址:https://www.cnblogs.com/huangzs/p/14034117.html
Copyright © 2020-2023  润新知