0

Just got a -1 for my answer on how to get the documents directory. I always do it like this:

NSString *documentsPath = [@"~/Documents" stringByExpandingTildeInPath];

And it always works as I expect it to. Are there any real underwater rocks with this method?

4
  • I guess 'Documents' can be considered Hardcode. Although I'm confident that it works +99 % of the time
    – Merlevede
    Feb 20, 2014 at 22:59
  • @Merlevede no, it works 100% of the time. And it's way more convenient than writing the routine of searching the documents directory. It doesn't really feel right, but it does the job perfectly fine
    – Eugene
    Feb 20, 2014 at 23:01
  • 1
    I believe you. Rigorously speaking, to be reliable it must be documented, there must be a place where it says that the documents directory will always be called 'Documents'
    – Merlevede
    Feb 20, 2014 at 23:04
  • The title says "iOS" but it's tagged with cocoa. Which one do you really mean? cocoa is for OS X. Perhaps it should be tagged cocoa-touch.
    – rmaddy
    Feb 20, 2014 at 23:12

2 Answers 2

1

No, it does NOT always work. If your app is running in sandbox (which is required by the app store), you won't be able to get the real documents directory.

I just did some test and below is what I got:

/Users/<user-name>/Library/Containers/<app-name>/Data/Documents

But my real document foder is

/Users/<user-name>/Documents
0

Right method to call: NSSearchPathForDirectoriesInDomains()

As Apple documentation states:

Creates a list of path strings for the specified directories in the specified domains. The list is in the order in which you should search the directories. If expandTilde is YES, tildes are expanded as described in stringByExpandingTildeInPath.

Example:

NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.