├── StoreIOS.js └── readme.md /StoreIOS.js: -------------------------------------------------------------------------------- 1 | export function StoreIOS() { 2 | } 3 | 4 | StoreIOS.prototype.append = function(file, obj) { 5 | var that = this; 6 | window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) { 7 | console.log('file system open: ', fs); 8 | var that1 = that; 9 | fs.root.getFile(file, {create: true, exclusive: false}, function (fileEntry) { 10 | console.log("fileEntry is file? " + fileEntry.isFile.toString()); 11 | that1.writeAtEnd(fileEntry, obj); 12 | }, function() {console.log('error: getFile')}); 13 | }, function() {console.log('error: requestFileSystem')}); 14 | }; 15 | StoreIOS.prototype.writeAtEnd = function(fileEntry, dataObj) { 16 | fileEntry.createWriter(function (fileWriter) { 17 | fileWriter.onwriteend = function() { 18 | console.log("Successful file write..."); 19 | }; 20 | fileWriter.onerror = function (e) { 21 | console.log("Failed file read: " + e.toString()); 22 | }; 23 | // go at the end of the file to append 24 | fileWriter.seek(fileWriter.length); 25 | fileWriter.write(dataObj); 26 | }); 27 | }; 28 | 29 | StoreIOS.prototype.getList = function(callbackList) { 30 | window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) { 31 | console.log('Get list of files; file system open: ', fs); 32 | var reader = fs.root.createReader(); 33 | reader.readEntries(function (entries) { 34 | callbackList(entries); 35 | }); 36 | }), function() {console.log('error')}; 37 | }; 38 | 39 | StoreIOS.prototype.read = function(file, callback) { 40 | window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) { 41 | fs.root.getFile(file, {create: false, exclusive: false}, function (fileEntry) { 42 | fileEntry.file(function (file) { 43 | var reader = new FileReader(); 44 | reader.onloadend = function() { 45 | console.log("Successful file read: " + this); 46 | callback(this); 47 | }; 48 | reader.readAsText(file); 49 | }, function() {console.log('error read')}); 50 | }) 51 | }); 52 | }; 53 | 54 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | 2 | The StoreIOS module is used to store and fetch documents on an iOS device through Phonegap. It uses the [Phonegap plugin File](http://docs.phonegap.com/en/edge/cordova_file_file.md.html). 3 | 4 | # Quick Start 5 | 6 | ## Write data 7 | 8 | ```javascript 9 | document.addEventListener("deviceready", function() { 10 | var storeIOS = new StoreIOS(); 11 | storeIOS.append('fileName1.txt', 'Ceci est un test'); 12 | }); 13 | ``` 14 | 15 | This will create a file named `fileName1.txt` containing 'Ceci est un test' in the `Documents` folder of the application. 16 | 17 | If the file `fileName.txt` does not exist it will be created. 18 | 19 | The file can be retrieve with iTunes if the option `Application supports iTunes file sharing` is set to `YES` in the application's `.plist` file. 20 | 21 | ## Read Data 22 | 23 | ```javascript 24 | document.addEventListener("deviceready", function() { 25 | storeIOS.read('fileName1.txt', callback); 26 | 27 | function callback(file) { 28 | // do something with the file 29 | document.getElementById('divToFill').innerHTML = file.result; 30 | } 31 | }); 32 | ``` 33 | 34 | This will read the file object `fileName1.txt` and use the `result` property to display its content in a HTML div. See the different properties and methods of the Reader object [here](http://docs.phonegap.com/en/edge/cordova_file_file.md.html#FileReader). 35 | 36 | The `read()` method requires the file name as first argument and the callback function using the file content as a second argument. 37 | 38 | ## Get file list 39 | 40 | ```javascript 41 | document.addEventListener("deviceready", function() { 42 | // create empty file list 43 | var files = []; 44 | storeIOS.getList(callback); 45 | 46 | function callback(fileNames) { 47 | for (var i=0; i 74 | ``` 75 | --------------------------------------------------------------------------------