NSSearchPathForDirectoriesInDomains returns the wrong directory

Josh Brown picture Josh Brown · Jun 29, 2011 · Viewed 11.1k times · Source

I'm using NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) to get the application documents directory in my app, but the array that's returned contains only one object, and it's an unwritable, incorrect directory:

/Users/me/Library/Application Support/iPhone Simulator/Documents

I found this question which indicates that the problem is related to provisioning, but the answer says little more than that. So I guess I'm asking a duplicate question, but the answer to that question is insufficient, so I'm hoping to get an actual answer in this one.

Answer

Rob picture Rob · Jul 6, 2011

Hmm ... so one reason that the directory returned might be different that what you'd expect for an app could be related to the Xcode target type. This wouldn't happen to be a testing target would it? in which case the correct answer could well be an answer w/o an application GUID in it, since in fact it's not an application. This google group discussion implies that if this is the case, you'd be good with simply creating the directory.

Just for grins, I created the directory /Users/me/Library/Application Support/iPhone Simulator/Documents from the terminal window, and now it appears to run. There are still test errors, but those might be real.

I'd recommend that you change your test app to create the documents directory if it's missing - something like:

  if(![[NSFileManager defaultManager] createDirectoryAtPath:NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) withIntermediateDirectories:YES attributes:nil error:NULL])
        NSLog(@"Error: Create folder failed %@", NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES));