ios清除缓存

作者: Tang_shuya | 来源:发表于2016-07-14 15:11 被阅读576次

    /*
   知识补充:应用沙盒(3部分)
       每个ios应用都有自己的应用沙盒(应用沙盒就是文件系统目录),与其他文件系统隔离。
   1.Documents:可以将应用程序数据储存在Document目录中,如果应用启用了Itunes文件分享功能,Itunes同步设备时会备份该目录.
   2.Library:保存应用运行时生成的需要持久化的数据,iTunes同步设备时不会备份该目录,一般储存体积大,不需要备份的数据.
   3.tmp:供应用储存临时文件,使用完毕后再将相应的文件从该目录删除,当ios设备同步时不会备份这个文件。

   模拟器沙盒目录:根目录
   NSString *homeDirectory = NSHomeDirectory();

   Documents路径:
   NSArray *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)  firstObject];

    Library路径:
    NSArray *libraryDirectory = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];  
    Library--Cache目录(这里存放的就是缓存文件)
    NSArray *cacheDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]; 
     Library--Cache--default(这里存放的是用户头像)
     NSArray *cacheDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"default"] ;

     tmp路径:
     NSString *tmpDir = NSTemporaryDirectory(); 
  */

    //获取缓存,清除缓存的操作比较耗时,理应开子线程去执行.
    //给NSObject封装一个分类,实现清除缓存的操作.

    //1.获取缓存
   //需要提供一个对象方法,外界去调用对象方法.
   - (void)getFileCacheSizeWithPath:(NSString *)path completion:(void (^)(NSInteger total))completion
   {
     [NSObject getFileCacheSizeWithPath:path completion:completion];
    }

   //写一个类方法
     // 异步方法使用回调,block
    + (void)getFileCacheSizeWithPath:(NSString *)path completion:(void(^)(NSInteger total))completion
   {
     // 开启异步线程 2秒
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
    // 1.创建文件管理者   
    NSFileManager *mgr = [NSFileManager defaultManager];
    
    // 1.1.判断下是否存在,而且是否是文件夹
    BOOL isDirectory; 
    BOOL isFileExist = [mgr fileExistsAtPath:path isDirectory:&isDirectory];
    
    // 判断下当前是否是文件
    if (isFileExist){
        // 判断下是否是文件夹
        NSInteger total = 0;
        if (isDirectory) {
            // 2.遍历文件夹下所有文件,全部加上,就是文件夹大小
            NSArray *subPaths = [mgr subpathsAtPath:path];
            for (NSString *subPath in subPaths) {
                // 3.拼接文件全路径
                NSString *filePath = [path stringByAppendingPathComponent:subPath];
                BOOL isDirectory;
                // 4.判断下当前是否是文件
                BOOL isFileExist = [mgr fileExistsAtPath:filePath isDirectory:&isDirectory];
                // 5.获取文件大小
                if (isFileExist && !isDirectory && ![filePath containsString:@"DS"]) {
                    
                    NSDictionary *fileAttr = [mgr attributesOfItemAtPath:filePath error:nil];
                    NSInteger fileSize = [fileAttr[NSFileSize] integerValue];
                    total += fileSize;
                } 
            }
            
        }else{
            // 当前传入是文件
            NSDictionary *fileAttr = [mgr attributesOfItemAtPath:path error:nil];
            total = [fileAttr[NSFileSize] integerValue];
        }
        
        // 计算完毕 -> 把计算的值传递出去
        dispatch_sync(dispatch_get_main_queue(), ^{
            
            if (completion) {
                completion(total);
            }
        });
    }
    });

   }

  //2.清除缓存
  + (void)removeCacheWithCompletion:(void (^)())completion
{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
    // 创建文件管理者
    NSFileManager *mgr = [NSFileManager defaultManager];  
    // 删除文件
    NSString *path = self.cachePath;
    BOOL isDirectory;
    BOOL isFileExist = [mgr fileExistsAtPath:path isDirectory:&isDirectory];
    if (!isFileExist) return;
    if (isDirectory) {
        NSArray *subPaths = [mgr subpathsAtPath:path];
        for (NSString *subPath in subPaths) {
            NSString *filePath = [path stringByAppendingPathComponent:subPath];
            [mgr removeItemAtPath:filePath error:nil];
        }
    }

     dispatch_sync(dispatch_get_main_queue(), ^{
        if (completion) {
            completion();
        }
    });
});

}

- (void)removeCacheWithCompletion:(void (^)())completion
 {
   [NSObject removeCacheWithCompletion:completion];

  }

 // 3.缓存路径
 - (NSString *)cachePath
 {
     return [NSObject cachePath];
 }

 + (NSString *)cachePath
   {
     // 获取cachePath文件路径
      return  [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
     }
    
    // 写的够详细了吧,至于外界怎么去调用,废话不多说了.

相关文章

  • WkWebView 清除缓存 ios8

    WkWebView 清除缓存 ios8iOS8系统NSString *libraryDir = NSSearchP...

  • 清楚WKWebView缓存

    WKWebView清除缓存WKWebView,在iOS9以后提供了缓存管理类WKWebsiteDataStore,...

  • iOS清除UIWebView缓存

    使用iOS的webview会自动进行缓存,在开发的时候要记得清除Cookie和缓存。

  • iOS 清除缓存

    iOS的缓存一般分为两部分,一部分是下载数据产生的缓存,这部分有系统做了缓存,在沙盒里面,还有一部分是图片的缓存,...

  • iOS清除缓存

  • 清除缓存ios

  • iOS清除缓存

    #pragma mark - 第一步,计算缓存文件的大小 //首先获取缓存文件的路径 -(NSString *)g...

  • ios清除缓存

  • iOS 清除缓存

    iOS 清除缓存 我们在使用任何一款APP的时候,无论是苹果的 还是安卓的 都会产生一些缓存 ,我们在使用APP的...

  • iOS 清除缓存

    我们在使用任何一款APP的时候,无论是苹果的 还是安卓的 都会产生一些缓存 ,我们在使用APP的时候 就要定期去清...

网友评论

    本文标题:ios清除缓存

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