Search code examples
iosxcodensfilemanager

Xcode Getting a Consistent Documents Directory Path


I've looked through the Apple documentation on this point and other questions here, but cannot find a means of getting a consistent path to the documents directory.

NSFileManager *fm = [NSFileManager defaultManager];
NSArray *urls = [fm URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];

NSURL *directory = [urls lastObject];

This produces a different path each time due to one component.

Example:

file:///var/mobile/Containers/Data/Application/CA708CF5-0E1B-414D-A795-31A8BB884BA5/Documents

Next run:

file:///var/mobile/Containers/Data/Application/2C96E341-85EF-485D-AC19-F8844B0880C3/Documents

I realize I need some kind of relative path here but I cannot figure out how to get it. How can I get to the Documents directory consistently to both write and read a file my app will produce?


Solution

  • The path is determined on installation. Each time you run your app in the simulator, it will be removed and reinstalled. Hence the differernt path. So you don't need to worry about this.