Q:

Javascript and Cordova/Phonegap FileWriter gives Invalid_Modification_Error on a simple text file write

I am using Cordova File and FileTransfer plugins. I have the following permissions set in my config.xml
<feature name="File">
    <param name="ios-package" value="CDVFile" />
</feature>
<feature name="FileTransfer">
    <param name="ios-package" value="CDVFileTransfer" />
</feature>

I am trying to write a plain text file using the following piece of code:
this.writeFile = function (filename, content, type, callback) {
   window.resolveLocalFileSystemURL(cordova.file.applicationDirectory, function(dir) { 
       dir.getFile(filename, { create: true }, function(fileEntry) {
            fileEntry.createWriter(function(fileWriter) {
                fileWriter.onwriteend = function(e) {
                    console.log('WRITE SUCCESS');
                    callback(null, e);
                };
                fileWriter.onerror = function(e) {
                    console.log('WRITE ERROR is');
                    console.log(e); 
                    callback(e);
                };

                var blob = new Blob([content], {type: type});
                fileWriter.write(blob);
            }, callback);
        }, callback);
    }, callback);
};

When I execute the line below:
this.writeFile('stuff.txt', 'Eggs and Burgers', 'text/plain', callback);

I get error while writing the file with code 9, which translates to INVALID_MODIFICATION_ERROR. What does this mean? Is it about permissions? How can I give filesystem permissions to my app in Xcode or Cordova?
Another issue is that my callback function gets called twice! Once for success and once for error. Now I understand that writeend does not necessarily mean a successful operation, but how to handle situations like this?
Note that this issue happens only on an actual iOS device. In iOS simulator, it runs and writes perfectly fine on the filesystem. After resolving how to write a text file, I will go on and write png/mp3/wav files as well.
I appreciate all the help. Thanks.

A:

Turns out there was nothing wrong with the code above, except that Apple forbids writing to app bundle after the bundle is installed. The bundle is signed and no modifications are allowed after that. 
To be able to write data, simply change
cordova.file.applicationDirectory

to
cordova.file.dataDirectory

and add the following to your config.xml according to your needs:
<preference name="iosPersistentFileLocation" value="Library" />

Your data then will be stored in Library/NoCloud directory in iOS.
Further reading on the iOS FileSystem:
https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html

