Created
January 8, 2014 08:21
-
-
Save keicoder/8313497 to your computer and use it in GitHub Desktop.
objective-c : Directory Paths on the iPhone and iPad
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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