【问题标题】:How to remove tmp directory files of an ios app?如何删除 ios 应用程序的 tmp 目录文件?
【发布时间】:2012-03-01 01:44:27
【问题描述】:

我正在开发一个使用 iPhone 摄像头的应用程序,经过多次测试后,我意识到它会将所有捕获的视频存储在应用程序的 tmp 目录中。 即使手机重启,截图也不会消失。

有什么方法可以删除所有这些捕获,或者有什么方法可以轻松清除所有缓存和临时文件?

【问题讨论】:

    标签: ios xcode file-io temp


    【解决方案1】:

    是的。这种方法效果很好:

    + (void)clearTmpDirectory
    {
        NSArray* tmpDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL];
        for (NSString *file in tmpDirectory) {
            [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL];
        }
    }
    

    【讨论】:

    • [[NSFileManager defaultManager] removeItemAtPath:NSTemporaryDirectory() error:NULL]; 怎么样?
    • @Itachi 该目录不应被删除。一些操作失败。
    【解决方案2】:

    Swift 3 版本作为扩展:

    extension FileManager {
        func clearTmpDirectory() {
            do {
                let tmpDirectory = try contentsOfDirectory(atPath: NSTemporaryDirectory())
                try tmpDirectory.forEach {[unowned self] file in
                    let path = String.init(format: "%@%@", NSTemporaryDirectory(), file)
                    try self.removeItem(atPath: path)
                }
            } catch {
                print(error)
            }
        }
    }
    

    使用示例:

    FileManager.default.clearTmpDirectory()
    

    感谢 Max Maier,Swift 2 版本:

    func clearTmpDirectory() {
        do {
            let tmpDirectory = try NSFileManager.defaultManager().contentsOfDirectoryAtPath(NSTemporaryDirectory())
            try tmpDirectory.forEach { file in
                let path = String.init(format: "%@%@", NSTemporaryDirectory(), file)
                try NSFileManager.defaultManager().removeItemAtPath(path)
            }
        } catch {
            print(error)
        }
    }
    

    【讨论】:

      【解决方案3】:

      斯威夫特 4

      一种可能的实现方式

      extension FileManager {
          func clearTmpDirectory() {
              do {
                  let tmpDirURL = FileManager.default.temporaryDirectory
                  let tmpDirectory = try contentsOfDirectory(atPath: tmpDirURL.path)
                  try tmpDirectory.forEach { file in
                      let fileUrl = tmpDirURL.appendingPathComponent(file)
                      try removeItem(atPath: fileUrl.path)
                  }
              } catch {
                 //catch the error somehow
              }
          }
      }
      

      【讨论】:

      • 应该试试 fileManager.contentsOfDirectory(atPath: tmpDirURL.path)
      • 它是一个扩展,所以不需要。
      • 我会为每个 removeItem 元素添加一个辅助 do-catch。如果一个元素卡住了,其余的将永远不会删除。
      • 当这是 FileManager 的扩展时,为什么要引用默认文件管理器?
      【解决方案4】:

      试试这个代码来删除 NSTemporaryDirectory 文件

      -(void)deleteTempData
      {
          NSString *tmpDirectory = NSTemporaryDirectory();
          NSFileManager *fileManager = [NSFileManager defaultManager];
          NSError *error;
          NSArray *cacheFiles = [fileManager contentsOfDirectoryAtPath:tmpDirectory error:&error];
          for (NSString *file in cacheFiles)
          {
              error = nil;
              [fileManager removeItemAtPath:[tmpDirectory stringByAppendingPathComponent:file] error:&error];
          }
      }
      

      在 didFinishLaunchingWithOptions 中检查数据删除或不写代码

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
          // Override point for customization after application launch.
          self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
          [self.window makeKeyAndVisible];
      
          NSString *tmpDirectory = NSTemporaryDirectory();
          NSFileManager *fileManager = [NSFileManager defaultManager];
          NSError *error;
          NSArray *cacheFiles = [fileManager contentsOfDirectoryAtPath:tmpDirectory error:&error];
          NSLog(@"TempFile Count ::%lu",(unsigned long)cacheFiles.count);
      
          return YES;
      }
      

      【讨论】:

        【解决方案5】:

        感谢 Max Maier 和 Roman Barzyczak。更新到 Swift 3,使用 URLs 而不是字符串。

        斯威夫特 3

        func clearTmpDir(){
        
                var removed: Int = 0
                do {
                    let tmpDirURL = URL(string: NSTemporaryDirectory())!
                    let tmpFiles = try FileManager.default.contentsOfDirectory(at: tmpDirURL, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
                    print("\(tmpFiles.count) temporary files found")
                    for url in tmpFiles {
                        removed += 1
                        try FileManager.default.removeItem(at: url)
                    }
                    print("\(removed) temporary files removed")
                } catch {
                    print(error)
                    print("\(removed) temporary files removed")
                }
        }
        

        【讨论】:

          【解决方案6】:

          这适用于越狱的 iPad,但我认为这也应该适用于未越狱的设备。

          -(void) clearCache
          {
              for(int i=0; i< 100;i++)
              {
                  NSLog(@"warning CLEAR CACHE--------");
              }
              NSFileManager *fileManager = [NSFileManager defaultManager];
              NSError * error;
              NSArray * cacheFiles = [fileManager contentsOfDirectoryAtPath:NSTemporaryDirectory() error:&error];
          
              for(NSString * file in cacheFiles)
              {
                  error=nil;
                  NSString * filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:file ];
                  NSLog(@"filePath to remove = %@",filePath);
          
                  BOOL removed =[fileManager removeItemAtPath:filePath error:&error];
                  if(removed ==NO)
                  {
                      NSLog(@"removed ==NO");
                  }
                  if(error)
                  {
                      NSLog(@"%@", [error description]);
                  }
              }
          }
          

          【讨论】:

          • 为了让你知道我在未越狱的 iphone ios6 上试过这个,效果很好。谢谢。
          猜你喜欢
          • 2012-03-28
          • 1970-01-01
          • 1970-01-01
          • 2014-09-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多