-1

i am storing my data in file manager in my app. now i want to delete specific data by code so how can i do this?

here is my code which i used for store data

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()
}}
Govind
  • 411
  • 1
  • 5
  • 11
  • NSFileManager has methods such as removeItemAtPath.. check the documentation - https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/ – Shripada Jan 07 '16 at 12:00
  • but i am saving data in session so it will be delet by removeItemAtPath? – Govind Jan 07 '16 at 12:37

1 Answers1

0

Not sure about session methods. Here's how to delete file in user temp directory, if that helps

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)
thinkswift
  • 528
  • 1
  • 5
  • 15