Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting iPhone Documents Directory. Is NSSearchPathForDirectoriesInDomains still the only way?

Is the NSSearchPathForDirectoriesInDomains function still the best way to get the path of the iPhone Documents directory? I ask because most topics I see on this are dated last year, and it still seems like a pretty cumbersome way of getting to a directory that is used commonly on iPhones. You'd think that there'd be a convenience method for this by now, similar to NSBundle's bundlePath, executablePath, etc.

Just to be clear, this means calling "NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)" and getting back an array with the Documents path at index 0.

like image 985
iPhoneToucher Avatar asked Dec 04 '09 03:12

iPhoneToucher


People also ask

How do I find the Documents folder on my Iphone?

Tap Browse at the bottom of the screen, then tap an item on the Browse screen. If you don't see the Browse screen, tap Browse again. To view recently opened files, tap Recents at the bottom of the screen. To open a file, location, or folder, tap it.

What is document directory in Swift?

The Documents directory is a better place to store data. We could append Documents to the path returned by the NSHomeDirectory() function, but it's a better idea to ask the FileManager class for the location of the Documents directory. This is slightly more verbose.


2 Answers

The Core Data-based application template in Xcode provides this method:

- (NSString *)applicationDocumentsDirectory {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}

So it would seem that Apple continues to endorse getting the documents directory that way. You could put it into a category, I suppose, but I have found it is enough to include that method in the small handful of classes in a given app that need to do work in the documents directory. If you're doing a lot of file stuff all over the place, you might consider refactoring your code a bit to confine those tasks to one or two manager classes.

For me, at least, the third or fourth time I said "Hey, getting the docs directory is a pain in the neck" was the point where I realized some opportunities to shift the file juggling into a dedicated class.

like image 97
Danilo Campos Avatar answered Nov 12 '22 21:11

Danilo Campos


The current Core Data iOS app template in Xcode provides this method:

// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
like image 36
Ivan Kovacevic Avatar answered Nov 12 '22 19:11

Ivan Kovacevic