Skip to content

Instantly share code, notes, and snippets.

@keicoder
Created January 8, 2014 08:21
Show Gist options
  • Select an option

  • Save keicoder/8313497 to your computer and use it in GitHub Desktop.

Select an option

Save keicoder/8313497 to your computer and use it in GitHub Desktop.
objective-c : Directory Paths on the iPhone and iPad
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];
//Objective-C class for the path of the documents directory
@interface Directories : NSObject {
}
+ (NSString*)documents;
+ (NSString*)home;
+ (NSString*)cache;
+ (NSString*)temporary;
@end
@implementation Directories
+ (NSString*)documents
{
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [arrayPaths objectAtIndex:0];
return docDir;
}
/* NSCachesDirectory */
+ (NSString*)temporary
{
return NSTemporaryDirectory();
}
+ (NSString*)home
{
return NSHomeDirectory();
}
+ (NSString*)cache
{
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDir = [arrayPaths objectAtIndex:0];
return cacheDir;
}
@end
//A simple usage example would be:
NSString* docs = [Directories documents];
NSString* filename = [docs stringByAppendingPathComponent:@"MyFileName.txt"];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment