Cordova file data directory aways change on iOS application update - ios

I need to download files, and store then in the dataDirectory. But When i start an application then i print:
console.log(cordova.file.dataDirectory);
file:///var/mobile/Containers/Data/Application/C5990CAA-439A-446E-A0B2-1212D3DC1072/Library/NoCloud/
When i build the application again, and prints the same console.log i got
file:///var/mobile/Containers/Data/Application/40C1C96E-E4EF-4F20-82C0-D13AA0385202/Library/NoCloud/

cordova.file.dataDirectory - Are a Persistent and private data storage within the application's sandbox using internal memory.
On each build on iOS the dataDirectory will be changed, but the files is "moved to new" folder. I dont found any documentation with tecnichal info.
To resolve you need save only de path after dataDirectory, for example
Your file will be safe in
file:///var/mobile/Containers/Data/Application/40C1C96E-E4EF-4F20-82C0-D13AA0385202/Library/NoCloud/user/files/image.png
You need save only user/files/image.png
To execute any operation with the file, copy, move, show you will use the cordova.file.dataDirectory + user/files/image.png

If someone has the same problem using Capacitor, I had to do this to be able to see my pictures again after an update
photo.webviewPath = Capacitor.convertFileSrc(this.file.dataDirectory + photo.fileName).replace('Library/NoCloud', 'Documents');

Related

Flutter-iOS when apps update / recompile stored image is missing

Hi forgive my English and also a newbie on flutter iOS, I have an app on flutter on iOS that users can take pictures and videos from their camera and gallery I'm using the image_picker package this has no problem, then I save it on the on its Application Documents Directory I tried following this guide
my code for getting the application directory is this
Directory _appDocDir;
_appDocDir = await getApplicationDocumentsDirectory();
final Directory _appMediaDir = Directory('${_appDocDir.path}/Pictures/');
return _appMediaDir;
added with a 'Pictures' directory this will give a path of
/Users/macman/Library/Developer/CoreSimulator/Devices/96060868-7E5C-4E15-9900-4D1D4B0B5ACE/data/Containers/Data/Application/BE7728A6-FF15-498E-91A0-A96A3DDE5B31/Documents/Pictures/D_IMG_0002.JPG
from the iOS-simulator I save it and put in the database, and after re-compiling the app or doing a build update the previewing the image/video path is not found anymore. I tried to save another image on this build it and gave this path
/Users/macman/Library/Developer/CoreSimulator/Devices/96060868-7E5C-4E15-9900-4D1D4B0B5ACE/data/Containers/Data/Application/9D634D91-07B8-43BE-8646-2A55833322DF/Documents/Pictures/D_IMG_0005.JPG
I noticed earlier path this part "/BE7728A6-FF15-498E-91A0-A96A3DDE5B31/" is changed every time I re-compile the app or make an update from the iOS device the image is not found. What is the most practice for this users able to save image am I doing it wrong?
Update: I tried changing my logic to change the saved path to only the base name (D_IMAGE_0005.JPG) then when previewing the image will call the current application documents directory but this does not work also.
Thank You
Hi don't save the whole path given by the getApplicationDocumentsDirectory() on you record, instead save only the basename, and every time you preview/load the image call the
getApplicationDocumentsDirectory() + 'Pictures/<basename>',
every time the app gets updated or recompiled on the iOS the appid changes, that is why your path fails to load on a recompile.
iOS removes the documents directory every time you reinstall the app if your application is running on a sandbox environment.
Please refer to this stackoverflow question for more clarification

How do I put content into the iOS Documents folder at compile time, using Xamarin?

I have a data file that I need to include with my app when I distribute it. When loading any files in the app, I prefix the file name with:
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
This works great for anything I create within the app (and for reading back), like files I download in response to a user action. But I can't for the life of me figure out how to place files there when I build my app in Visual Studio.
I've tried making a "Documents" subdirectory in the special "Resources" folder, but that didn't work (I tried setting the "Build Action" to both BundleResource and Content). When I look at the folder for my app (from using the simulator) I can see that in the "Documents" folder there's all the files I downloaded, but I can't find my data file that I'm trying to bundle ahead of time. I even searched my entire hard drive on the Mac and still couldn't find said data file.
The data file isn't an image, if it matters. Just raw binary data. How do I set it up so that this file goes into the proper documents directory at compile time, so that I can read it using the SpecialFolder.MyDocuments prefix? Thanks.
You can't. You can include files in your app bundle, and then at startup copy them from the bundle into a user folder. But this won't happen automatically.

resolveLocalFileSystemURL on iOS using different App UUID each time

I am using Cordova fileSystem to download some mp3 Audio files thru my App. I am using cordova.file.cacheDirectory and creating a subfolder within that to store the downloaded files. However, the problem is that each time I make some changes and run the App on iPad I get a different path.
The usual pattern is
file:///var/mobile/Containers/Data/Application/13E9EA83-94A2-4950-811E-E491AC176‌​A78/Library/Caches/MyFolder/MyFiles.mp3
However the long number (App UUID) is not consistent. It changes each time I run the App on iPad through Xcode. Because of this I am unable to access, play, delete any files downloaded during the previous session because I am saving the file names with absolute path in a separate file for quick access in the future.
After some R&D I have realized that even though UUID changes each time, it actually refers to the same location. Hence the solution is not to save absolute path and instead, save only the Path beyond "/MyFolder/" in the above example. Each time when the App is run, the Base Path, which is actually file.cordova.cacheDirectory, should be appended to each entry to obtain the absolute Path to the file.

iOS storing files rules - Storing audio files on iOS app

I need to be sure I am doing the right thing:
In my app the user can download audio files from the server. I don't want those big file to be backed up as he can re download them when ever he needs to.
My app is addressed for iOS 4.0 and above.
So as I understand I need to store the files is the documents directory and set a flag for the directory to not back up ?
Am I correct ?
Instead of putting them into the "Documents" directory (which gets backed up to the cloud), why not put your audio files into a "Cache" directory (specifically "/Library/Cache", which does not get backed up)?
Here's another question here on Stackoverflow that may help give a further answer to your question.
You can also prevent files to backed up:
https://developer.apple.com/library/ios/qa/qa1719/_index.html

Where to store the SQLite database file?

I have a SQlite database file with records which comes with my app.
I just used the file I added to my project to make my INSERT and UPDATE statements.
After uploading it to my test device I got the exception that the file is read only.
After a bit of research I found out, that I have to copy the db file to the users directory to make an insert. This works for now. But I have a view questions about it, which i didn't get answered through google:
Where should I put my copy process?
I implemented it in the AppDelegates FinishedLaunching, where I check if it already exists.
Where should I copy the file to?
I used the MyDocuments folder for now, is this ok?
Since the file cannot be encrypted, can another app access the database file?
When the user decides to delete the app from the device. Will the database file get deleted,too?
Thanks!
Where should I put my copy process? I implemented it in the AppDelegates FinishedLaunching, where I check if it already exists.
That really depends, but finishedLaunching is OK from my point of view.
Where should I copy the file to? I used the MyDocuments folder for now, is this ok?
I'm not sure what you mean by "MyDocuments" folder. Each Application has a dedicated Document directory. That's where you should copy it.
Since the file cannot be encrypted, can another app access the database file?
No, they run sand-boxed (unless the device is jailbroken)
When the user decides to delete the app from the device. Will the database file get deleted,too?
Yes, since the whole document directory will be deleted.
Where should I put my copy process? I implemented it in the AppDelegates FinishedLaunching
Keep in mind that you have a limited amount of time to complete FinishedLaunching execution (around 15 seconds) before the iOS watchdog kills your application.
Depending on the size of your database, the speed of device and other processing you need to do then you might want to consider only checking if it already exists (that should be quick) then do the copy (if required) from to another thread.
I used the MyDocuments folder for now, is this ok?
Yes, using Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments) is correct.
Everything else from #Lightforce answer covers the rest.

Resources