Directory Paths on the iPhone and iPad

The correct way to retrieve the path of the documents directory in iOS is, according to Apple Documentation:

    NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(
					NSDocumentDirectory,
					NSUserDomainMask,
					YES);
    
    NSString *docDir = [arrayPaths objectAtIndex:0];

I never seem to be able to remember this when I need it. So I wrapped this and other directory related routines into a simple Objective-C class. Here’s what the interface looks like (I’ve removed the doxygen documentation for brevity):

@interface IDZDirectories : NSObject {
}
+ (NSString*)documents;
+ (NSString*)home; 
+ (NSString*)cache;
+ (NSString*)temporary;
@end

A simple usage example would be:

NSString* docs = [IDZDirectories documents];
NSString* filename = [docs stringByAppendingPathComponent:@"MyFileName.txt"];

Now this is not an earth-shattering piece of code, but it may save you some typing. You are free to download it and use it in your own projects.

Download
IDZDirectories.zip

About idz

A professional software engineer dabbling in iOS app development.
This entry was posted in Code Snippets and tagged . Bookmark the permalink.

Leave a Reply