SoFunction
Updated on 2025-04-09

IOS cache file clear implementation code

In the application development of mobile Internet APPs, you must always pay attention to user experience to avoid causing the APP or mobile phones and other mobile devices to get stuck. The following is the processing of cached files.

When mobile applications process network resources, they generally perform offline cache processing, among which image cache is the most typical, and the most popular offline cache framework is SDWebImage.

However, offline cache will occupy the storage space of your phone, so the cache cleaning function has basically become a standard feature for information, shopping, and reading apps.

The implementation of offline caching functions introduced today is mainly divided into the implementation of obtaining cache file sizes and clearing cache files.

1. Get the cache file size

-( float )readCacheSize
{
  NSString *cachePath = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES) firstObject];
  return [ self folderSizeAtPath :cachePath];
}

 

Because cache files are in the sandbox,We can passNSFileManager APITo calculate cache file size。
// traverse the folder to obtain the folder size, how much M is returned- ( float ) folderSizeAtPath:( NSString *) folderPath{
  
  NSFileManager * manager = [NSFileManager defaultManager];
  if (![manager fileExistsAtPath :folderPath]) return 0 ;
  NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator];
  NSString * fileName;
  long long folderSize = 0 ;
  while ((fileName = [childFilesEnumerator nextObject]) != nil ){
    //Get full path of file    NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName];
    folderSize += [ self fileSizeAtPath :fileAbsolutePath];
  }

  return folderSize/( 1024.0 * 1024.0);
  
}

 

// Calculate the size of a single file- ( long long ) fileSizeAtPath:( NSString *) filePath{
  NSFileManager * manager = [NSFileManager defaultManager];
  if ([manager fileExistsAtPath :filePath]){
    return [[manager attributesOfItemAtPath :filePath error : nil] fileSize];
  }
  return 0;
}

 

2. Clear the cache

- (void)clearFile
{
  NSString * cachePath = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES ) firstObject];
  NSArray * files = [[NSFileManager defaultManager ] subpathsAtPath :cachePath];
  //NSLog ( @"cachpath = %@" , cachePath);
  for ( NSString * p in files) {
    
    NSError * error = nil ;
    //Get full path of file    NSString * fileAbsolutePath = [cachePath stringByAppendingPathComponent :p];
    
    if ([[NSFileManager defaultManager ] fileExistsAtPath :fileAbsolutePath]) {
      [[NSFileManager defaultManager ] removeItemAtPath :fileAbsolutePath error :&error];
    }
  }
  
  //Read cache size  float cacheSize = [self readCacheSize] *1024;
   = [NSString stringWithFormat:@"%.2fKB",cacheSize];

}


 

Thank you for reading, I hope it can help you. Thank you for your support for this site!