美文网首页
iOS 清除App缓存

iOS 清除App缓存

作者: ios_暗夜行者 | 来源:发表于2023-11-20 19:59 被阅读0次

    //计算缓存大小

    - (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)

    相关文章

      网友评论

          本文标题:iOS 清除App缓存

          本文链接:https://www.haomeiwen.com/subject/gigvwdtx.html