如何从我的iPhone的应用程序内存中删除文件。来自文件经理ios swift

时间:2016-01-07 10:03:09

标签: ios swift swift2 ios9 nsfilemanager

我将数据存储在我的应用程序的文件管理器中。现在我想通过代码删除特定数据,那么我该怎么做呢?

这是我用于商店数据的代码

var localURL : String

init()
{

    let urls = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
    localURL = urls.first!.URLByAppendingPathComponent("podcasts").path!
    createDirectory(localURL)
}

func downloadShow(slug: String, show: NSDictionary) {

    SVProgressHUD.showWithStatus("Downloading...")
    let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: sessionConfig, delegate: nil, delegateQueue: nil)
    let url = NSURL(string: show["file"] as! String)
    let request = NSMutableURLRequest(URL: url!)
    request.HTTPMethod = "GET"
    let task = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
        if (error == nil) {
            let showFileName = url?.lastPathComponent
            let programMP3Path = self.localURL + "/" + slug + "/" + showFileName!
            let programDataPath = programMP3Path + ".dat"
            data?.writeToFile(programMP3Path, atomically: true)
            show.writeToFile(programDataPath, atomically: true)


            print("Success")
            print(showFileName)
           SVProgressHUD.dismiss()
        }
        else {
            // Failure
            print("Faulure: \(error)");
        }
    })
    task.resume()
}}

1 个答案:

答案 0 :(得分:0)

不确定会话方法。以下是如何删除用户临时目录中的文件,如果有帮助的话

let myFileName = "myFile.txt"
var fileManager = NSFileManager()
var tempDirectory = NSTemporaryDirectory()
let filePath = tempDirectory.stringByAppendingPathComponent(myFileName)
var error: NSError?

// also good idea to check before if the file is in the directory        

let path = tmpDir.stringByAppendingPathComponent(isFileInDir)
fileManager.removeItemAtPath(path, error: &error)