简体   繁体   中英

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.

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.

You Can Create A new folder for your app, inside document Directory Try Following code to create a folder. And don't forgot to read from same directory when you want data on app.

And when you don't want to provide access to other user then you either have to delete that folder on logout or have to make folder with name userId or some other unique key . I recommend deleting that particular folder on logout.

 let yourPathToDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String + "/" + "YourAppDirDir" + "/"

    if !fileManager.fileExists(atPath: yourPathToDirectory!) {
        do {
            try  FileManager.default.createDirectory(atPath: yourPathToDirectory!, withIntermediateDirectories: true, attributes: nil)
        } catch  {
            print("error found")
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM