1

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

5
  • Check this link ,it should fix your problem plis it will show you the normal parh of an image: Commented Jan 20, 2020 at 14:56
  • forum.ionicframework.com/t/… Commented Jan 20, 2020 at 14:57
  • Thank you for the response. In the link you provided it looks like they specified where the file would be download - cordova.file.dataDirectory . In my case, I download a text file from a gmail account on the iPhone. Looking at the iPhone it is stored at - Files Icon - "On My iPhone", I do not know the path to this location? As I mentioned below I tried reading the file at the following locations documentsDirectory, dataDirectory, syncedDataDirectory, cacheDirectory, tempDirectory per github.com/apache/cordova-plugin-file#file-system-layouts but the file is not found. Commented Jan 20, 2020 at 16:55
  • You can access the file manually right? Commented Jan 20, 2020 at 17:01
  • On the iPhone I can go to the Files Icon and I see the text file under the "On My iPhone". I can double click on it and it opens. When I try to use for example this.file.readAsText(this.file.documentsDirectory, 'MyData.txt').then((data) => {}). It cannot be found. I am not sure how to figure out the path to the file? Commented Jan 20, 2020 at 17:22

2 Answers 2

4

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');
}); 
Sign up to request clarification or add additional context in comments.

Comments

1

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/>

3 Comments

Thank you for the response Sergey. I believe I have already tried your suggestion. I had downloaded from a gmail account a text file to my iPhone. It is stored in Files icon - "On My iPhone". I then proceeded to try to read the file in from the various locations (documentsDirectory, dataDirectory, syncedDataDirectory, cacheDirectory, tempDirectory) as follows - this.file.readAsText(this.file.documentsDirectory, 'MyTextFile.txt').then((data) => { console.log(data); }).catch((err) => { console.log('documentsDirectory file doesnot exist ' + err); });
Yeah interesting, I read this one: stackoverflow.com/questions/55372276/… so it very much can be that accessing files created by other apps (gmail?) is subject to permissions etc hence not out of box available to cordova plugin which works well when your app creates and reads its own files
Thank you for the link. It looks like you are correct. One suggestion at the link was to add the following to the info.plist - <key>UIFileSharingEnabled</key> <true/> I will try this. Thanks for finding the link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.