cordova-ionic ngCordova ios or iPhone file read er

2020-07-13 09:29发布

问题:

i am using cordova-ionic framework to build app. i am new to the iOS or iPhone in my requirement, i have to read a file in the app. i am reading file in the android app but same code showing error (code: 5).

i am following code types:

in android:

$cordovaFile.writeFile(( 'user.json', data, {'append':false} )).then(function(result) {
  alert('file created.');
    alert(JSON.stringify(result));
}, function(err) {
    // An error occured. Show a message to the user
    alert('file writed');
    alert(JSON.stringify(err));
});

i can create file, writing, reading data and removing the file but in ios phone i am not able to create file using the same code.

in iPhone:

var data = {"user":{"name":"errer","email":"sdsdff@gmail.com","username":"sdfsdfsd"}};

$cordovaFile.writeFile(( 'user.json', data, {'append':false} )).then(function(result) {
    // Success!
    alert('file created.');
    alert(JSON.stringify(result));
}, function(err) {
    // An error occured. Show a message to the user
    alert('file writed');
    alert(JSON.stringify(err));
});

i just change my directory is cordova.file.cacheDirecotry/cordova.file.applicationDirectory

$cordovaFile.createFile(( cordova.file.cacheDirecotry+'user.json', true )).then(function(result) {
    // Success!
    alert('file created.');
    alert(JSON.stringify(result));
}, function(err) {
    // An error occured. Show a message to the user
    alert('file writed');
    alert(JSON.stringify(err));
});

all way getting the error like code: 12 or code: 5

please help me to solve this or give me a idea to get application file path

回答1:

Refer to the original documentation :- https://github.com/apache/cordova-plugin-file/blob/master/doc/index.md#ios-file-system-layout

iOS has some directories as read-only. Try changing your path.

Let me know if it does not work for you.



回答2:

I have some progression.

First, I alert my cordova.file.dataDirectory or cordova.file.documentsDirectory. They are

file:///var/mobile/...../Library/NoCloud 

and

file:///var/mobile/..../Documents

Then I create a File without the prefix and succeed. Referring to this https://github.com/driftyco/ng-cordova/issues/362

and the success message shows that the native url of the file is saved in

file:///var/mobile/...../Library/files

Which is quite strange. By the way, I add the

<preference name="iosPersistentFileLocation" value="Library" />

according to https://github.com/apache/cordova-plugin-file/blob/master/doc/index.md#ios-persistent-storage-location

All the tests are running on IOS, i haven't test for Android.

Updates

All the following code worked for me and give success response

$cordovaFile.checkFile('/test.data')

$cordovaFile.createFile('test.data',false)

$cordovaFile.checkDir('/')

Hope this can solve your problems.



回答3:

/*
Here is what I am using for my Android and IOS apps
Keep attention to a couple of things:
-	Android and IOS have other directorynames for files
-	Android devices have different root (myFSRootDirectory1 = Samsung Tab 3, msFSRootDirectory2 = Samsung SII)
-	$cordovaFile functions prefixes all pathnames with root
$cordovaFileTransfer functions needs absolute pathnames

Here I create the prefixes for File functions and FileTransfer functions for Android and IOS
*/

// The $ionicPlatform and ionic.Platorm are from Ionic framework
//  

$ionicPlatform.ready(function() {
	if (ionic.Platform.isAndroid()) {
// If running on Android
console.log('cordova.file.externalDataDirectory: ' + cordova.file.externalDataDirectory);
//
// I use cordova.file.externalDataDirectory because this url is for Android devices
// If you remove the app from the device these url are cleared too on the device. So keep it clean.
// Remove the root from cordova.file.externalDataDirectory
// 
			myFsRootDirectory1 = 'file:///storage/emulated/0/'; // path for tablet
			myFsRootDirectory2 = 'file:///storage/sdcard0/'; // path for phone
			fileTransferDir = cordova.file.externalDataDirectory;
			if (fileTransferDir.indexOf(myFsRootDirectory1) === 0) {
				fileDir = fileTransferDir.replace(myFsRootDirectory1, '');
			}
			if (fileTransferDir.indexOf(myFsRootDirectory2) === 0) {
				fileDir = fileTransferDir.replace(myFsRootDirectory2, '');
			}
console.log('Android FILETRANSFERDIR: ' + fileTransferDir);
console.log('Android FILEDIR: ' + fileDir);
		}
		if (ionic.Platform.isIOS()) {
// if running on IOS
console.log('cordova.file.documentsDirectory: ' + cordova.file.documentsDirectory);
// I use cordova.file.documentsDirectory because this url is for IOS (NOT backed on iCloud) devices
			fileTransferDir = cordova.file.documentsDirectory;
			fileDir = '';
console.log('IOS FILETRANSFERDIR: ' + fileTransferDir);
console.log('IOS FILEDIR: ' + fileDir);
		}

	if (ionic.Platform.isAndroid() || ionic.Platform.isIOS()) {
//
// Just functions from the list below one by one ( or chain them)
//
	}
});



// Download file from 'http://www.yourdomain.com/test.jpg' to test/one/test.jpg on device Filesystem
var hostPath = 'http://www.yourdomain.com/test.jpg';
var clientPath = fileTransferDir + 'test/one/test.jpg';
var fileTransferOptions = {};

$cordovaFile.downloadFile(hostPath, clientPath, true, fileTransferOptions).then (function() {
});
// Create dir test
$cordovaFile.createDir(fileDir + 'test/').then( function(dirEntry) {
});
// Create dir aganin in dir test
$cordovaFile.createDir(fileDir + 'test/one/').then( function(dirEntry) {
});
// Create empty file test.txt in test/again/
$cordovaFile.createFile(fileDir + 'test/one/test.txt', true).then( function(fileEntry) {
});
// List of files in test/again
$cordovaFile.listDir(fileDir + 'test/one/').then( function(entries) {
console.log('list dir: ', entries);
});
// Write some text into file 
$cordovaFile.writeFile(fileDir + 'test/one/test.txt', 'Some text te test filewrite', '').then( function(result) {
});
// Read text written in file
$cordovaFile.readAsText(fileDir + 'test/one/test.txt').then( function(result) {
console.log('readAsText: ', result);
});



回答4:

Perhaps it's because of a typo? You have cordova.file.cacheDirecotry. Shouldn't that be : cordova.file.cacheDirectory ?