How to remove tmp directory files of an ios app?

前端 未结 6 1001
囚心锁ツ
囚心锁ツ 2020-12-05 02:18

I\'m working on an app that uses the iPhone camera and after making several tests I\'ve realised that it is storing all the captured videos on the tmp directory of the app.

6条回答
  •  忘掉有多难
    2020-12-05 02:35

    Swift 3 version as extension:

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

    Example of usage:

    FileManager.default.clearTmpDirectory()
    

    Thanks to Max Maier, Swift 2 version:

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

提交回复
热议问题