If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
As many of you may have seen by now, there are quite a few ways to find the documents directory on the iPhone. For those of you who don’t know, the documents directory of an app is the location where you should save your application data. While finding the documents directory is a trivial task, it is very important when coding most applications. Apple has provided quite a few ways for resolving the path to this directory.
If you read through some of Apple’s sample code, you will see their code to do this is generally 3 or 4 lines long. This seems like a lot of code to perform such a simple task. Here is a nice one-liner for you to use in your applications.
NSString * docs = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
It’s clean and concise. Happy iCoding!


9 Comments
Which of course fails to work on non-English platforms as the “Documents” directory is localised to the language in question and is not always “Documents” therefore.
I would tend to go with how apple does it rather than this way. There is usually a method to their madness.
@Alex – Are you sure? Apple renames the documents directory of an app based on the language?
The name of the directory will be the same for all languages. Only the display name is translated to the selected locale. But, there is no way to be sure that the name of the directory will be always “Documents” for all versions of the os. So, hardcoding such values is always a bad idea.
any ideas on how to test if a file exists? I download files store them to a path I create by using.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"csv.txt"];
I would like to know if csv.txt is already there.
Thanks
I tried the following but it did work for me
FILE* testFile = fopen(path, “rb”);
if (testFile) {
NSLog(@”found file”);
}
Here’s the proper one-liner:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
@Michael
“But, there is no way to be sure that the name of the directory will be always “Documents” for all versions of the os.” Are you serious? Renaming the Documents directory in an OS iteration sounds very Microsoft. Why would anyone do that?
Many, probably most, of these are chemical engineers or technicians rather than chemists per se. ,