Error: The file...doesn't exist when calling writeToFile on imageData

Instead of [saveLocation absoluteString], use [saveLocation path]. Basically the former gives you "file:///path/filename" while the latter gives you "/path/filename", which is the correct format.


Got Woking.. Thank you @superstart swift code below :

let documentsUrl =  NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first
let fileNameString = fileURL.absoluteString.stringByReplacingOccurrencesOfString("/", withString: "");
let destinationUrl = documentsUrl!.URLByAppendingPathComponent("check.m4a")

let request: NSURLRequest = NSURLRequest(URL: fileURL)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
{ (response: NSURLResponse?, datas: NSData?, error: NSError?) in
    if error == nil
    {
       // datas?.writeToFile(destinationUrl.absoluteString, atomically: false);

        do
        {
            let result = try Bool(datas!.writeToFile(destinationUrl.path!, options: NSDataWritingOptions.DataWritingAtomic))
            print(result);
        }
        catch let errorLoc as NSError
        {
            print(errorLoc.localizedDescription)
        }
    }
    else
    {
        print(error?.localizedDescription);
    }
}

You may be having an issue with an intermediate directory that doesn't exist. If any sub directories (folders) don't exist when writing a file, it will fail.

Here is a handly function to create a single directory of any name in the documents folder. If you try to write to this after running this it should be fine.

static func createDirIfNeeded(dirName: String) {
        let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent(dirName + "/")
        do {
            try FileManager.default.createDirectory(atPath: dir.path, withIntermediateDirectories: true, attributes: nil)
        } catch {
            print(error.localizedDescription)
        }
    }