Search code examples
swiftdebuggingdirectoryfilenamesdocuments

Durable documents folder?


To read and save data from my App I want to use documents folder of the user. When debugging, this is mapped to somewhere else. But I want to have the "real" user document folder. I want to avoid to use a fixed path name to my user when debugging. Actually I'm using

if let dirs : [String] = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as? [String] {
        let dir = dirs[0]   // documents directory
        return dir.stringByAppendingPathComponent(filenameOnly)
    }

where filenameOnly is the file I like to access.


Solution

  • You are doing it right. (except, as Rob says in his comment, you should use the user domain instead of allDomains.)

    On iOS, and on Mac OS for a sandboxed app (all new App store apps must be sandboxed) you only have access to certain blessed sandboxed directories.

    The documents directory will persist for the life of your app. If you submit an upgrade through the app store the documents directory and it's contents will persist.

    The code you posted gives you a path to the "real" documents directory. If you are running an iOS app on the sim, you'll get a path on your Mac. If you're running on the device, you'll get a bizarre looking path inside your app's bundle.