18

This Apple tech note:

http://developer.apple.com/library/ios/#qa/qa2010/qa1699.html

suggests storing "internal" user documents in a subdirectory off of ~/Library. But I can't find one of the pre-created search domains that would get me this. What's the best/most correct/least likely to ever break way of constructing this path?

Thanks.

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204

2 Answers2

43

The correct way is

NSString* path;
path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];

However, [@"~/Library" stringByExpandingTildeInPath] also works.


Swift 3:

let path = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)[0]
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • NSlibraryDirectory is documented to point to the root /Library, not the user version. Are they semantically equivalent in this case? – Ben Zotto Sep 21 '10 at 19:42
  • 1
    I have confirmed that `NSLibraryDirectory` combined with `NSUserDomainMask` does the right thing here. Thanks Kenny. – Ben Zotto Sep 22 '10 at 16:17
6

You can also try this:

[NSHomeDirectory() stringByAppendingString:@"/Library"]
Vidhur
  • 94
  • 1
  • 3