Removing cache files when app is removed ios - 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

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

Can I create a Documents directory in Xcode, and put files in it for my iOS app through the UI. If so, how?

I need to read/write a tiny amount of info for data persistence in an iOS app. I am using plists and understand that I can read, but not write to the bundle. If I create the plist files initially at development time, how can I place the plists in the "Documents" area of my app so that they are later writable?
Short answer: No.
As you say, an iOS app's bundle is not writable, but it is readable.
Further, an app's documents directory doesn't exist until the app is run (on the user's target device) for the first time. Thus, you can't build your app's documents directory at compile-time, nor can you submit an app to the app store with a prepopulated Documents directory. (It would be nice if Apple gave us the ability to provide a "seed" documents directory that got moved to the documents directory at first launch, but they do not provide that facility -- at least not that I'm aware of.)
What you can do is to create a sub-directory of your bundle that contains a set of files that you want to place in the app's documents directory.
On launch, check a "hasBeenInstalled" flag in UserDefaults. If it's true, do nothing. if it's false, use the file manager to copy files from your app's bundle into the app's documents directory, then write hasBeenInstalled=true to UserDefaults.

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

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.

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