简体   繁体   中英

ionic 4 - iOS file access with @ionic-native/file/ngx/cordova-plugin-file

I am trying to read in a text file that is sent via e-mail and then downloaded to an iOS device. Specifically the text file is saved in - “Files icon - On My Phone”.

I am using the following plugin @ ionic-native/file/ngx / cordova-plugin-file . I am able to get this to work on Android but I am not sure of the to the location of the file or how to find it?

I have used the plugins checkDir() on the following iOS directories to verify that they exist and that I can use the installed plugin:

cordova.file.tempDirectory - Temp directory that the OS can clear at will. Do not rely on the OS to clear this directory; your app should always remove files as applicable. ( iOS , OSX , windows )

cordova.file.syncedDataDirectory - Holds app-specific files that should be synced (e.g. to iCloud). ( iOS , windows )

cordova.file.documentsDirectory - Files private to the app, but that are meaningful to other application (e.g. Office files). Note that for OSX this is the user’s ~/Documents directory. ( iOS , OSX )

I want to use the following method to access and read in the text file:

this.file.readAsText()

But if I try to access the file in each of these directories - for example via the following:

this.file.readAsText(this.file.tempDirectory, 'MyData.txt').then((data) => {})

It errors out with not being able to locate the file.

Bottom line is I do not have the correct path to the text file stored on my iOS device.

Any help on how to determine the path to my text file saved on an iOS device would be greatly appreciated.

Thanks

I finally figured it out.

I had to add BOTH the following key value pairs to my.info.plist:

<key>UIFileSharingEnabled</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>

In Xcode this will appear as:

Application supports iTunes file sharing | Boolean | YES
Supports opening documents in place | Boolean | YES

I had seen some other posts where each one of the key value pairs was mentioned but never needing both. I tried each individually multiple times and it would not work without both key value pairs added to the info.plist of the application.

A new folder will be created under the Files icon \ "On My iPhone". The folder name will correspond to your app name. There is a sub-folder under this new app folder - NoCloud.

I was able to place my text file in this location ("On My iPhone"\MyAppName\NoCloud) and read in the text file from my app using the following code:

const path = this.file.documentsDirectory + '/NoCloud';
this.file.readAsText(path, 'MyFile.txt').then((data) => {
    console.log(data);
}).catch((err) => {
    console.log('File does not exist');
}); 

I would refer to the plugin's github file locations reference: https://github.com/apache/cordova-plugin-file#file-system-layouts

Then check where actually such.txt gets stored after you save it from email (I think it might also depend on which email client is used and which iOS version, in particular, you intend to support)

Update: for IOS specifically one should add file sharing permission via config.xml:

<key>UIFileSharingEnabled</key>
<true/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM