Accessing The App Documents folder, The Swift Way

ios_app_layout_2xWhen you create a new iOS application on your Mac, the operating system installs it in a sandbox directory, which contains the application bundle directory and three additional directories, Documents, Library, and tmp. The application’s sandbox directory, often referred to as the home directory, can be accessed via code or Finder.

By the way, the OS used to create the application sandbox in this directory, on your Mac’s hard drive:

~/Library/Application/iPhone Simulator

Now, it is created in this directory:

~/Library/Developer/CoreSimulator

Accessing The App Documents Folder Via Code

Now, to retrieve and display the full path to an application’s Documents directory, you’d have to add this code snippet in a Swift file’s function; for example, the viewDidLoad() function.

let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
let documentsDir = paths.firstObject as String
println("Path to the Documents directory\n\(documentsDir)")
Code Output
Path to the Documents directory
/Users/yourname/Library/Developer/CoreSimulator/Devices/E568C36B-48ED-416F-B374-451CB0F4ADA7/data/Containers/Data/Application/C9EB59E5-3290-4BD6-BF0E-081F43ADD4A8/Documents
Accessing The App Documents Folder Via Finder

Now that you’ve gotten the full path to the application’s Documents folder. You can use Finder’s menu (Go > Goto Folder…) to navigate to that path. You’ll have to enter the path in Finder’s Goto Folder box like this:

~/Library/Developer/CoreSimulator/Devices/E568C36B-48ED-416F-B374-451CB0F4ADA7/data/Containers/Data/Application/C9EB59E5-3290-4BD6-BF0E-081F43ADD4A8/Documents

Once you click the Go button, Finder will take you straight to the application’s Documents folder.

finder-app-documents-folder

That’s it. Now you know two ways to access your iOS application’s Documents folder.

  • Pingback: [基本解决]iOS里面的app可以使用的用于保存文件的路径和文件夹有哪些 – 在路上()

  • Eladio Alvarez Valle

    There any way to pre-populate documents Directory?, I need to have pre-loaded data in there for initial settings, and then download data in there. Thanks in advance App lADY.

  • Juan

    Hi! Nice post. I got a question related to this. I have a json file on my Bundle files and I need to create a path to access my Document Folder (where I have downloaded some resources) do you know anyway to create this path?
    My json when reads from bundle looks like that:
    “jsList” : [
    “src/resource.js”,
    “src/app.js”
    ]
    I was thinking to create something like that:
    “jsList” : [
    “/var/mobile/Containers/Data/Application/1F50D5BE-C2ED-4629-AEF6-5C8D9381FF68/Documents/src/resource.js”,
    “/var/mobile/Containers/Data/Application/1F50D5BE-C2ED-4629-AEF6-5C8D9381FF68/Documents/src/app.js”
    ]

    But without success.
    Any ideas???

    Thanks a lot!

  • Carver

    How do you know which app is which? The directory doesn’t list app names, just the long string of character names.