Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to store private important user data when the Documents directory is not an option?

My app is using iTunes file sharing which exposes everything in the Documents directory to the user, making it vulnerable to accidentally deletion or manipulation.

I spent hours in reading though these documents but it is a mess and I hope someone knows from experience. First, in one place they say that I should put these files in the Library directory.

In this technical Q & A Apple says that this is preserved. From my understanding this means that I can safely put important user data like sqlite3 database files in this directory. When the user updates to a new version, the content inside this directory will be preserved, it will survive and be available after the update:

applications can create their own directories in /Library/ and those directories will be preserved in backups and across updates

So /Library/ is preserved in backups and across updates.

For me with bad english this means: YES, the data will survive. It will not be lost when the user backs up. It will not be lost when the user updates. I looked up the word "preserved" in several dictionaries and I am sure it means "it will survive".

But then, there is this note in the iOS Application Programming Guide which tells something completely different! Here, they say about the Library directory:

<Application_Home>/Library/
You should not use this directory for user data files.
The contents of this directory (with the exception of the Caches
subdirectory) are backed up by iTunes.
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.

"Should not use for user data files." (???)

But at the same time they admit it's backed up by iTunes. OK. So why shouldn't I put user data files into there, then?

/Library/Caches Use this directory to write any application-specific support files that you want to persist between launches of the application or during application updates. (...) It should also be able to re-create these files as needed because iTunes removes them during a full restoration of the device.

What?! I should put these files in Library/Caches. But this directory is not backed up by iTunes, as they said above. So this is only save for updates, but not for backup. And the data might be deleted anytime by the system.

Now this is completely confusing me. From what I understand I can choose between the evil and the devil: Data in /Library/ does not survive updates but is backed up by iTunes. Data in /Library/Caches does survive updates, but is not backed up by iTunes AND it might be deleted by the system (since it's a "Cache") anytime.

And on the other hand, the technical Q & A suggests putting this important user data in a custom subfolder in /Library/, for example /Library/PrivateDocuments.

In contrast to the iOS Application Programming Guide, the technical Q & A says: the entire /Library directory has always been preserved during updates and backups

So now, really, one of both documents MUST be wrong. But which one? What's the truth? Please, not speculation! I'm looking for answers from experience and I feel there is no way to figure this out except releasing an app and praying. Maybe someone wants to share his/her experience what really worked.

like image 682
dontWatchMyProfile Avatar asked May 24 '11 17:05

dontWatchMyProfile


3 Answers

I've seen Library/Preferences (where NSUserDefaults are stored) be kept across restores, so I think most of Library is kept. The cache directories are probably excluded, though.

In general, just use the APIs to fetch paths and trust that iTunes will preserve them unless they're meant to represent temporary folders. That means you should use a subdirectory of your NSApplicationSupportDirectory named for your application:

NSArray * urls = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask];
NSAssert([urls count], @"Can't get app support directory");

NSURL * url = [urls objectAtIndex:0];
url = [url URLByAppendingPathComponent:@"MyAppName"];

In practice, this will end up being "Library/Application Support/MyAppName" in your sandbox, but you should use the API anyway to ensure this is kept future-proof.

(If you care about support for iOS 3 or 2, use the NSSearchPathForDirectoriesInDomains() function instead of the -URLsForDirectory:inDomains: method.)

like image 149
Becca Royal-Gordon Avatar answered Oct 04 '22 00:10

Becca Royal-Gordon


Why don't you try the keychain? If your data is not too extensive it can provide a quick way to store sensitive information

Keychain Documentation from Apple

like image 32
arclight Avatar answered Oct 04 '22 00:10

arclight


The content inside Library folder (except cache) is backup by itunes.

What apple mean by "Should not use for user data files." (???) is that don't use this folder for data you want to be view in the File Sharing sytem via itunes.

So If you want the user to access the data via itunes write to /Document folder

If you want to hide the data from the user write to /Library folder

like image 41
Sinuhe Huidobro Avatar answered Oct 04 '22 01:10

Sinuhe Huidobro