Accessing the Document directory on iOS

The Documents directory in which an App can store information that should be available at a future point. It can be used to store documents of any data type.

One challenge can be the way that you refer to the location of the Documents directory in your App's code. Ideally, we will be able to use URLs, in the format of the NSURL class. There are times when we need to refer to the path. This post describes how to do both of these using Swift.

URL for the Documents Directory

The first option is to get the path as a URL. To do this, we access the NSFileManager object. We use the static method defaultManager() to get an instance of NSFileManager and then use URLsForDirectory to get an array of URLs. The result has the type [NSURL].

NSFileManager.defaultManager().URLsForDirectory(
   .DocumentDirectory, 
   inDomains: .UserDomainMask)

The first URL that is returned in that array will refer to the Documents directory. This can be accessed as:

NSFileManager.defaultManager().URLsForDirectory(
   .DocumentDirectory, 
   inDomains: .UserDomainMask)[0]

If we were to inspect this or print it out, we would see something like:

file:///var/mobile/Containers/Data/Application/10608BD1-D2F4-41A4-A708-4D449906EE7B/Documents/

The long number, between Application and Documents is a number that represents the application on the device.

To refer to a specific file in the Documents directory, we can use URLByAppendingPathComponent() on the NSURL. An example is shown below.

let documentsURL = NSFileManager.defaultManager().URLsForDirectory(
   .DocumentDirectory, 
   inDomains: .UserDomainMask)[0]
let fileURL = 
   documentsURL.URLByAppendingPathComponent("imageDetails")

If we were to inspect the above, we would see the following:

file:///var/mobile/Containers/Data/Application/10608BD1-D2F4-41A4-A708-4D449906EE7B/Documents/imageDetails

Path for the Documents Directory

The second option is to get the path to the item in the Documents directory. To do this, we can use the method NSSearchPathForDirectoriesInDomains, which is part of Apple's Foundation Functions.

The following method call will obtain a list of paths. The result of this method is an Array of Strings, [String].

NSSearchPathForDirectoriesInDomains(
   .DocumentDirectory,.UserDomainMask,true)

The Documents directory is the first item in the returned array. So, the path can be obtained as

let path = NSSearchPathForDirectoriesInDomains(
   .DocumentDirectory,.UserDomainMask,true)[0]

If we print this out, we get a value such as the following. As above, the long number, between Application and Documents is a number that represents the application on the device.

/var/mobile/Containers/Data/Application/10608BD1-D2F4-41A4-A708-4D449906EE7B/Documents/

To refer to a specific file in the Documents directory, we can use stringByAppendingString() from NSString. We pass in the name of the file that we want to access, such as the following examples.

NSSearchPathForDirectoriesInDomains(
   .DocumentDirectory,.UserDomainMask,true)
   [0].stringByAppendingString("imageDetails")

That will generate us a String that represents the path.

/var/mobile/Containers/Data/Application/10608BD1-D2F4-41A4-A708-4D449906EE7B/Documents/imageDetails

Summary

As you can see, both methods are similar. Where possible, use the URL format. Apple's documents state that this is the preferred option. However, there are times when the API you are using requires a path instead of the URL.

Context
This post is written in relation to iOS 9.x.