How to remove tmp directory files of an ios app?

前端 未结 6 1008
囚心锁ツ
囚心锁ツ 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:33

    Thanks to Max Maier and Roman Barzyczak. Updated to Swift 3, using URLs instead of strings.

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

提交回复
热议问题