iOS 清除App缓存

2023-11-20  本文已影响0人  ios_暗夜行者

//计算缓存大小

- (CGFloat)getSize {

    NSUIntegersize =0;

    NSFileManager *fileManager = [NSFileManager defaultManager];

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

    NSDirectoryEnumerator*fileEnumerator = [fileManagerenumeratorAtPath:cachePath];

    for(NSString*fileNameinfileEnumerator) {

        NSString*filePath = [cachePathstringByAppendingPathComponent:fileName];

        NSDictionary*attrs = [fileManagerattributesOfItemAtPath:filePatherror:nil];

        size += [attrsfileSize];

    }

    //将大小转化为M

    returnsize /1024.0/1024.0;

}

/*

  12-23 update: use removeItemAtPath to delete each subdir

  instead of delete the root dir and then recreate it

*/

- (void)clearFile

{

    NSFileManager *fileManager = [NSFileManager defaultManager];

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

    NSLog(@"%@",cachePath);

    NSDirectoryEnumerator*fileEnumerator = [fileManagerenumeratorAtPath:cachePath];

    for(NSString*fileNameinfileEnumerator) {

        NSString*filePath = [cachePathstringByAppendingPathComponent:fileName];

        [fileManagerremoveItemAtPath:filePatherror:nil];

    }

}

参考:关于 iOS 删除缓存的那些事儿 - 简书 (jianshu.com)

上一篇下一篇

猜你喜欢

热点阅读