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

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

Related

Offload App remove what in iOS 11

I just installed iOS 11 beta and I noticed that Offload App, By offloading app what will be remove Shared Directory or User Default or remove both or other than this ?
Looking for suggestions.
Thanks
Offloading lets you ”offload” the bundle (.ipa) for your app to save disk space. It will delete the ipa file (which contains your libraries and resources) and store only Documents and Data that includes shared data, UserDefaults etc..
Once we offload the app, what remains is the app icon of it with a small download pip on it to indicate that it has been offloaded .
Just by simple tapping on it, the app will get downloaded from the store and the saved Documents and Data will be integrated to the downloaded app, instead of an empty directory as it normally would with a fresh install.
This is a really great feature by Apple especially for devices with just 16GB storage. As you can see offloading the Prisma app saves me 44MB which is great. And even more, iOS can automatically offloads unused apps for us.
There is no official document(technical) available for offloading apps till the date but as the description(in setting app) says that your documents and data will be saved then I think it will save user defaults , document directories and core data - I mean all kind of data that can be stored!
It will save Shared Directory & User Defaults. Everything else will be deleted.

Can you access previous /var files from new compiles of the same app?

Our app downloads a bunch of video files and allows the user to view them. We've noticed that we can download and play the files with no issues at all. But then if an update is pushed or we use Xcode to push a new app over the old one (same version) it will report that the video cannot be found. This is despite the the fact that we can see the video there, in the same folder and folder ID we're looking for has not changed. Is there something happening behind the scenes with local storage for iOS app files that makes them inaccessible after an update?
Example:
/var/mobile/containers/data/application/{same id}/documents/

Removing cache files when app is removed ios

I'm wondering about removing cache files I've stored inside my app. The directory is made in Documents, beyond the app's folder. Is it possible to write code inside app that will be executed when iOS removes the app from system? Can it be done without storing files beyong app's cache directory?
Thank for any answer. I'm working in emulator so what path should be to make directory in app tmpFolder to not take care about leftovers after deleting app?
you aren't informed about an app's deinstallation / removal BUT nothing except iCloud and keychain survives the removal - especially not some app's caches
your app runs in a sandbox with its own library, caches and documents folder

What directory should I store analytics data in on iOS?

I'm trying to store analytics data that is saved locally about a user's actions so it can be uploaded later when the user has an internet connection. I'd like the data to be stored locally and not deleted between subsequent opens of the app under normal circumstances. I do not want the data to be synced to iCloud. I'd also ideally like the data to be preserved between updates. It's fine if the data gets deleted in cases of low space.
I'm getting different answers from different sources about where to store the data- either in NSCachesDirectory or NSLibraryDirectory. Note NSCachesDirectory is a subdirectory of NSLibraryDirectory, eg. the filesystem looks like Application_Home/Library/Caches/.
According to the official documentation: http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/PerformanceTuning/PerformanceTuning.html#//apple_ref/doc/uid/TP40007072-CH8-SW9 implies I should use NSCachesDirectory to store the data and it is not deleted under most circumstances. It also implies NSLibraryDirectory is synced to iCloud.
According to these answers: How can I get a writable path on the iPhone?, https://stackoverflow.com/a/5444762/340520/, When are files from NSCachesDirectory removed?, NSCachesDirectory is not preserved between app updates and claims that I must constantly recreate the NSCachesDirectory. The first answer also implies NSLibraryDirectory is the best place to store the data. However those answers are two years old.
According to the documentation and this answer: http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/PerformanceTuning/PerformanceTuning.html#//apple_ref/doc/uid/TP40007072-CH8-SW10, https://stackoverflow.com/a/8830746/340520, NSCachesDirectory ARE preserved between app updates.
Localytics' iOS SDK stores their data in NSCachesDirectory: https://github.com/twobitlabs/Localytics-iOS/blob/master/src/LocalyticsDatabase.m
Mixpanel's iOS SDK stores their data in NSLibraryDirectory: https://github.com/mixpanel/mixpanel-iphone/blob/master/Mixpanel/Mixpanel.m
Between all these sources, I've been leaning toward using NSCachesDirectory, but I'm not confident that the data won't get regularly deleted under some circumstances.
NSCachesDirectory is the wrong place to store persistent information that you will need across app starts or even device re-starts.
To prove my point try this ...
Get a iPhone device that has only 8GB disk space.
Use your app to write a file in the NSCachesDirectory.
Start downloading random apps to fill up the disk space. Very soon you will see the Storage limit dialog shown by the OS.
Now just re-start your phone, start the app and see if you can find your file that you wrote.
If the first time you find the file, try the experiment again and you will find your file missing. During the device startup, if you see the device logs you will notice logs indicating purging directory to make space etc.
Use NSCachesDirectory to store information you can keep downloading from your server. Not information that you need to upload to your server.
The Caches directory should only be used for files that your app can easily replace if they are deleted. The Caches directory is may or may not be purged during an app update and possibly if the device runs out of storage space. Only use this for temporary files or files you can easily replace.
My first choice would be the Library/Application Support directory (NSApplicationSupportDirectory). Please note that this directory is not created by default. Your app must create it on first startup. This path is kept during app updates (like most of the app sandbox) and it is backed up via iTunes (or iCloud) device backup.
I think the best place is Library/Application Support (NSApplicationSupportDirectory) refer How do I prevent files from being backed up to iCloud and iTunes? for details.

ios native app allowed to save data from web?

We're building an iPad photo gallery with hundreds of images (almost 300mb in size). Having a web app will be a disadvantage because users will have to download the images every time (since Safari on ipad won't cache them I'm guessing?)
So if we built a native ios app instead, would it be possible to download newer images from the web and add it to the app in the future?
Thanks a ton for your help !
Each iPhone application has its own home directory containing Library and Documents folders, and a tmp directory for storing temporary files.
Take a look at section title: A Few Important Application Directories on The Application Runtime Environment.
I would save the your images to Application Home/Library/Caches folder.
Excerpt from Apple Docs:
Use this directory to write any application-specific support files that you want to persist between launches of the application or during application updates. Your application is generally responsible for adding and removing these files. It should also be able to re-create these files as needed because iTunes removes them during a full restoration of the device.
In iOS 2.2 and later, the contents of this directory are not backed up by iTunes.
http://iosdevelopertips.com/data-file-management/save-uiimage-object-as-a-png-or-jpeg-file.html
you could download it on first view, display at the uiimage, then save it off as above

Resources