ndreisg ndreisg -4 years ago 83
iOS Question

User specific Documents directory on iOS device

In my iOS app I want to store some user specific data.

I expected

NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
to return the Documents directory of the user, but instead it returns the following directory:
/var/mobile/Containers/Data/Application/%app_id%/Documents


When i log in with another user (Apple-ID) on my iOS device, this path stays exactly the same.

It seems like this directory is only user specific in Mac OS.

So which directory should I use to store user specific data in iOS?

UPDATE:

According to the previous answers and comments, the desired behaviour doesn't seem to be intended.

Answer Source

From Apple Documentation

Where You Should Put Your App’s Files

Put user data in Documents/. User data generally includes any files you might want to expose to the user—anything you might want the user to create, import, delete or edit. For a drawing app, user data includes any graphic files the user might create. For a text editor, it includes the text files. Video and audio apps may even include files that the user has downloaded to watch or listen to later.

Put app-created support files in the Library/Application support/ directory. In general, this directory includes files that the app uses to run but that should remain hidden from the user. This directory can also include data files, configuration files, templates and modified versions of resources loaded from the app bundle.

Remember that files in Documents/ and Application Support/ are backed up by default. You can exclude files from the backup by calling -[NSURL setResourceValue:forKey:error:] using the NSURLIsExcludedFromBackupKey key. Any file that can be re-created or downloaded must be excluded from the backup. This is particularly important for large media files. If your application downloads video or audio files, make sure they are not included in the backup.

Put temporary data in the tmp/ directory. Temporary data comprises any data that you do not need to persist for an extended period of time. Remember to delete those files when you are done with them so that they do not continue to consume space on the user’s device. The system will periodically purge these files when your app is not running; therefore, you cannot rely on these files persisting after your app terminates.

Put data cache files in the Library/Caches/ directory. Cache data can be used for any data that needs to persist longer than temporary data, but not as long as a support file. Generally speaking, the application does not require cache data to operate properly, but it can use cache data to improve performance. Examples of cache data include (but are not limited to) database cache files and transient, downloadable content. Note that the system may delete the Caches/ directory to free up disk space, so your app must be able to re-create or download these files as needed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download