How to create a usable csv file in iOS?


#1

I am working on an app which allows users to enter data into it, which generates a graph based on numerical data they entered.

The user has the option to back up this data as a CSV file saved to their device. If they ever needed to uninstall the app, but didn’t want to lose their data, they can import a previously-created CSV data to get it all back.

For importing, I am using angular-csv-import like so: <ng-csv-import class="import-file" content="csv.content" header="csv.header" separator="csv.separator" result="csv.result" accept="csv.accept"></ng-csv-import>
This creates an <input type="file"> element on the page, and this is monitored with $scope.$watch to trigger the import process whenever the filename changes.

For exporting, my function looks something like this (contained inside another function which receives the results from CSV.stringify:

window.appRootDirName = ".myapp";
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
	fs.root.getDirectory(
		window.appRootDirName,
		{
			create: true,
			exclusive: false
		},
		function(dirEntry) {
			if (ionic.Platform.isIOS()) {
				var filePath = cordova.file.syncedDataDirectory;
			}
			else {
				var filePath = dirEntry.nativeURL;
			}
			$cordovaFile.createFile(filePath, reportTitle, true).then(function() {
				return $cordovaFile.writeFile(filePath, reportTitle, result, true); //where "result" is the csv data that was passed to this function
			}).then(function(result) {
				deferred.resolve(reportTitle + " exported successfully.<br>");
			}, function(err) {
				deferred.reject(err);
			});
		}
	)
});

On Android, this creates no issues. When a CSV file is created, it creates a folder called .myapp (if it doesn’t already exist) in the root of the device storage. After uninstalling and reinstalling the app, the user can use the import feature to navigate to this file and trigger the import process.

On iOS, no such luck. File-type inputs only give the option of choosing between iCloud Drive and device photos, and sometimes iCloud Drive does not show up as an option at all. Often when I choose iCloud Drive, I am prompted to set it up, and after doing so, it disappears as an option for file-type inputs. This behaviour is incredibly strange.

I have been trying to figure out how to export this CSV to iCloud Drive. Using cordova.file.syncedDataDirectory as the export directory should theoretically work, but after the export function indicates successful completion, I open the iClud Drive app and look in my Drive and it is nowhere to be found, there is no folder with the name of my app anywhere and there are no files related to it.

I have been searching for a solution for over a week now. Any insight is greatly appreciated.


#2

@stefk any luck on this? I am looking for a solution like this.