This is part of an ELC Tech Network

Code Snippet – Quickly Find The Documents Directory

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"];
Advertisement

It’s clean and concise. Happy iCoding!

This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

9 Comments

  1. Posted September 9, 2009 at 9:46 am | Permalink

    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.

  2. Posted September 9, 2009 at 10:12 am | Permalink

    I would tend to go with how apple does it rather than this way. There is usually a method to their madness.

  3. Posted September 9, 2009 at 11:11 am | Permalink

    @Alex – Are you sure? Apple renames the documents directory of an app based on the language?

  4. Michael
    Posted September 9, 2009 at 11:20 am | Permalink

    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.

  5. layne
    Posted September 11, 2009 at 7:01 pm | Permalink

    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

  6. layne
    Posted September 11, 2009 at 7:06 pm | Permalink

    I tried the following but it did work for me

    FILE* testFile = fopen(path, “rb”);
    if (testFile) {
    NSLog(@”found file”);
    }

  7. squidbot
    Posted September 15, 2009 at 12:19 pm | Permalink

    Here’s the proper one-liner:

    [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]

  8. rey
    Posted September 15, 2009 at 3:47 pm | Permalink

    @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?

  9. Posted October 22, 2009 at 5:00 pm | Permalink

    Many, probably most, of these are chemical engineers or technicians rather than chemists per se. ,

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">