What is the correct place to permanently cache data so it's available until the user chooses to remove it

I'm looking to get my iOS app to cache file data in a place where the user cannot access it directly but it stays cached until the user chooses to clear it.


The ideal solution would be to a place where it appears in "Settings > iPhone Storage > AppName > Recommendations" even though I already provide a mechanism to clear it from within the app.


At the moment I'm caching the data into "/var/mobile/Containers/Data/Application/EF5B1643-C28D-4D0B-8DC6-23FFF1BEE4B8/Documents/../Library/Preloads" but it seems to intermittently get cleared by the OS.


Looking at the following document https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html this seems to be a valid location that shouldn't be getting cleared automatically by the OS.


Where should I be writing cache files so they:


1) Are not directly accessible to the users

2) Appear in the Settings > iPhone Storage > AppName > Recommendations section

3) Stay cached until the user specifically chooses to clear them (either from the app or the Recommendations section) and are not cleared automatically by the OS

4) Are not backed up by iTunes or iCloud

Accepted Reply

I use the Application Support directory for that sort of thing. Documents is intended for user visible files. But neither is cleared by the OS unless the app is deleted by the user. The absolute path may change between runs of your app, and if you’re talking about the simulator all bets are off, but on the real device files will persist. As long as you use the correct APIs to get the path to the directory, files should always be there until your code deletes them or the user deletes the app.


As for the “not backed up” part, files in those directories are backed up by default, but you can set a flag in code to exclude files or directories from backups. See https://developer.apple.com/library/archive/qa/qa1719/_index.html

Replies

37 views and no replies, is this posted in the wrong forum?

I use the Application Support directory for that sort of thing. Documents is intended for user visible files. But neither is cleared by the OS unless the app is deleted by the user. The absolute path may change between runs of your app, and if you’re talking about the simulator all bets are off, but on the real device files will persist. As long as you use the correct APIs to get the path to the directory, files should always be there until your code deletes them or the user deletes the app.


As for the “not backed up” part, files in those directories are backed up by default, but you can set a flag in code to exclude files or directories from backups. See https://developer.apple.com/library/archive/qa/qa1719/_index.html

That's really helpful junkpile, thank you. Do you have any idea about how to get them to appear in the "Settings > iPhone Storage > AppName > Recommendations" section so they can be cleared from there too?

I took a look on my phone and the only apps I see that have "Recommendations" items are first-party (Apple) apps, namely Photos and Messages. *None* of the third-party apps I use regularly have that option. I don't recall reading about any API to provide custom recommendations to free up storage - I don't think it's available to third-party apps. It would be nice to be wrong though.

I don't recall reading about any API to provide custom recommendations to free up storage …

While there’s no general-purpose API for this, there is one mechanism I’m aware of, namely the HTTP Live Streaming offline storage management mechanism. See WWDC 2017 Session 504 Advances in HTTP Live Streaming.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"