iOS application cannot access downloaded file when relaunched - UUID changes - ios

I've been debugging a problem where my app successfully downloads a file and copies it to the Documents directory. Lately the app cannot open the downloaded file on subsequent launches of the app. I've been printing out file URLs and this is what I see when the file is initially downloaded and opened:
unzipped /var/mobile/Containers/Data/Application/9AC69C00-228E-482F-99D8-DD8F214FCE88/Documents/3.atcase to
/var/mobile/Containers/Data/Application/9AC69C00-228E-482F-99D8-DD8F214FCE88/Library/Application Support/thebundleID/current-case
You can see the UUIDs are the same, but when I launch the app again I see this:
couldn't unzip /var/mobile/Containers/Data/Application/9AC69C00-228E-482F-99D8-DD8F214FCE88/Documents/3.atcase to
/var/mobile/Containers/Data/Application/C18E7EFA-C0D0-4213-AF85-F5BC0D2A4207/Library/Application Support/thebundleID/current-case
This time the UUIDs appear to be different.

I will say tentatively that the answer is yes, the UUID used in the application container folder does seem to change every time the app is run, though I'd love to have some confirmation from a more experienced developer.
The solution to my problem was straightforward: Do not store absolute file URLs in the database. The URLs for the files are now constructed relative to the Documents directory URL every time the app is run. Since I name the files in a regular way, there is no need to store URLs anyway.

Related

How do you persist image references across app updates on iOS devices

I have developed a Flutter app that captures images using the camera and I store references to the image files using Shared Preferences.
When I upgrade the iOS app, the Shared Preference filename persists as expected, but the image no longer displays on the iOS device (and no longer seems to exist) File(_imageFileRef).existsSync() is false
For example, on iPhone, the image file is saved as
/private/var/mobile/Containers/Data/Application/580A9879-23CD-413D-A785-DB910673DF74/tmp/some_guid_image_name.jpg
When the app is upgraded, this file no longer seems to exist.
Where should I be saving the image files to in iOS so that they persist across upgrades?
The functionality works perfectly on Android devices.
Having received no answers, I delved a bit deeper and discovered that...
...the tmp directory in which the images are being written is for temporary files that do not need to persist between launches of your app. Your app should remove files from this directory when they are no longer needed; however, the system may purge this directory when your app is not running. The contents of this directory are not backed up by iTunes or iCloud.
So in order for the data to persist over app updates, I need to be writing to the Documents directory
Info obtained from here

Having a bit of trouble with copying a local file on iOS

Having only worked with Android/web dev before I am having a hard time figuring out where in the world my file should be getting copied to in iOS. After reading the file browser documentation on the iOS developer resource page it says that each app is an "island" which contains its own folder system. If I want to have my user be able to copy a file from my app's sdk to their iOS device, where should I put it?
I downloaded a file browser app from the app store on my iOS testing device but am I right in thinking that there is no global file browsing system?
I am using a Cordova plugin called Asset2sd which works perfectly for me in Android, getting the root storage folder and downloading the file to there. It has no iOS alternative so I'm going to have to figure something else out, I just need to know where to start. Do I have to have the user access my app's internal files somehow? Can I create a folder for them to look in when they download my app? Some documentation or something would be wonderful. I am totally lost.
Thanks!
You're right. Apps don't have access to the global file system. Each app only has access to their own app directory. Here's in-depth information on Sandboxing: https://developer.apple.com/app-sandboxing/
In your app's sandbox, you basically can manage files as you want, i.e. download, copy, move, delete, etc. So you can create a folder Downloads in your apps documents directory and then display the contents of this folder for instance in a table view.
Related documentation/links:
https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/index.html
https://stackoverflow.com/a/6907432/967327
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/iPhoneAppProgrammingGuide.pdf

IOS - In-app purchase downloadable media storage guidelines

I have an app which has in app purchase downloadable videos. The first time I submitted to the app store it was rejected because I was writing it to <Application_Home>/Documents directory. The rejected reason stated that
In particular, we found that on launch and/or content download, your
app stores 20MB.
I read the guideline and it told me that
Data that can be downloaded again or regenerated should be stored in
the <Application_Home>/Library/Caches directory. Examples of files you
should put in the Caches directory include database cache files and
downloadable content, such as that used by magazine, newspaper, and
map applications.
I checked the simulator's cache directory and found that there is a default folder within the Caches folder with the name of the Bundle Id of the app. So I changed it to the following path
<Application_Home>/Library/Caches/<Bundle ID>/
Ran this in the sim and seems fine. However, when I uploaded it, it was rejected. The problem seems to come from writing the data.
Question: Is there something wrong with this directory? Should I just store it in the Caches root folder?
Thanks.

Store images in iOS app directory when install

I am writing an iOS app which download images from a backend server, and store them in the document directory. I know how it is done.
But I need to write a prototype first, which use some pre-given images (not retrieve from the backend server). My question is, is it possible to store images in the document directory during initial installation of the app? I don't want to hardcode them in the bundle directory.
Thanks
Nothing can be done when the app is installed, only when the app is run. The first time the app runs, you can either copy pre-bundled images to the other directory (Documents) or you can download them as needed. It sounds like you want the first option for the prototype.
Basically you need to do what you don't want to do - hardcode them in the bundle.
Remember, the app's sandbox is setup when the app is installed. This includes the creation of the Documents directory. But nothing is put there. Only your running app can store something somewhere in the app's sandbox.

Releasing IOS app with files in Library directory

I have an app that is going to display a list of local video files. The app will only have a couple of video files at first, but that number will grow with future updates and in-app purchases. I would like to store all these videos in 1 place and it makes sense to put them in the app's Library directory so they are not user-accessible. Is it possible to publish an app with files already in the app's Library directory? Or do I have to put them in the app's bundle and then copy them to the Library directory when the app is first launched? Although that would result in the 2 videos files being duplicated since I can't remove them from the bundle.
Yes, you do need to copy the files from the app bundle into the Library if you want to treat all the videos in the same way. You cannot change the contents of the app bundle from inside your app and the only files your app has when it first launches are those that are in the app bundle.
If you really wanted (and if the video files don't change), you could make special cases in your code to be able to get the starting videos from the app bundle and the in-app purchase downloaded videos from the Library directory. But, unless the video files are quite large this seems a bit excessive.
A third option would be to have the user download the starter files on first app launch, but that may be suboptimal for other reasons.
Please look at my answer in this thread. It might concern you or at least be interesting to read.
Does updating iOS apps delete library directory files?

Resources