问题
Does anyone know if the iOS "Files" application data can be accessed via Cordova? I.e., I want to add my application to the list of "Locations" in the Files application, and then read/write those files. Is this possible?
Thanks!
回答1:
You must set iCloud under Capabilities in Xcode. Don't forget to set the Container for your App Identifier.
Maybe this option will help you in future:
LSSupportsOpeningDocumentsInPlace
回答2:
I also had this issue and resolved it. Here's what I did: To give the files app access to my app's data, I had to open the info.plist file (just search for "info.plist" in your iOS platform folder) and add the following snippet:
<key>UIFileSharingEnabled</key>
<true/>
Now, when you click on your Ressources > info.plist file in xcode, you should have this entry:
Screenshot Data Access
After building your app to your device, the files app should then give you access to "On My (Device)" and list all folders and files you created using the cordova file plugin.
回答3:
Cordova-plugin-file implements a File API allowing read/write access to files residing on the device.
iOS Persistent storage location
There are two valid locations to store persistent files on an iOS device: the Documents directory and the Library directory. Previous versions of the plugin only ever stored persistent files in the Documents directory. This had the side-effect of making all of an application's files visible in iTunes, which was often unintended, especially for applications which handle lots of small files, rather than producing complete documents for export, which is the intended purpose of the directory.
It is now possible to choose whether to store files in the documents or library directory, with a preference in your application's
config.xml file
. To do this, add one of these two lines toconfig.xml
:
<preference name="iosPersistentFileLocation" value="Library" />
<preference name="iosPersistentFileLocation" value="Compatibility" />
Write to File
Once you have a FileEntry object, you can write to the file by calling createWriter
, which returns a FileWriter object in the success callback. Call the write method of FileWriter to write to the file.
function writeFile(fileEntry, dataObj) {
// Create a FileWriter object for our FileEntry (log.txt).
fileEntry.createWriter(function (fileWriter) {
fileWriter.onwriteend = function() {
console.log("Successful file write...");
readFile(fileEntry);
};
fileWriter.onerror = function (e) {
console.log("Failed file write: " + e.toString());
};
// If data object is not passed in,
// create a new Blob instead.
if (!dataObj) {
dataObj = new Blob(['some file data'], { type: 'text/plain' });
}
fileWriter.write(dataObj);
});
}
Read a file
You also need a FileEntry object to read an existing file. Use the file property of FileEntry to get the file reference, and then create a new FileReader object. You can use methods like readAsText
to start the read operation. When the read operation is complete, this.result stores the result of the read operation.
function readFile(fileEntry) {
fileEntry.file(function (file) {
var reader = new FileReader();
reader.onloadend = function() {
console.log("Successful file read: " + this.result);
displayFileData(fileEntry.fullPath + ": " + this.result);
};
reader.readAsText(file);
}, onErrorReadFile);
}
来源:https://stackoverflow.com/questions/55372276/can-cordova-read-write-files-in-the-ios-files-app