Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to find the user's Documents directory on an iPhone?

I'm reading Erica Sadun's iPhone Developer's Cookbook, and ran into a question.

She says in the book that the way to find the user's Documents directory is with the code:

[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 

but that seems slightly brittle, and dissimiliar to the normal Mac way of doing it, which would be:

NSSearchPathForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES); 

Are there any particular reasons to use one over the other?

like image 646
bwinton Avatar asked Nov 07 '08 16:11

bwinton


People also ask

What is documents directory in iOS?

Every iOS app gets a slice of storage just for itself, meaning that you can read and write your app's files there without worrying about colliding with other apps. This is called the user's documents directory, and it's exposed both in code (as you'll see in a moment) and also through iTunes file sharing.

Can you search documents on iPhone?

You can do a Control-F search on an iPhone's browser by using the "On This Page," "Find in Page," or Share features. Control-F is a computer shortcut that locates specific words or phrases on a webpage or document. You can search for specific words or phrases in Safari, Google Chrome, and Messages.

How do I manage documents on my iPhone?

Touch and hold the file or folder, then choose an option: Copy, Duplicate, Move, Delete, Rename, or Compress. To modify multiple files or folders at the same time, tap Select, tap your selections, then tap an option at the bottom of the screen.


2 Answers

Objc:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) 

Swift:

var paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) 

You'll want the first element of the returned array.

like image 186
Ben Gottlieb Avatar answered Oct 02 '22 14:10

Ben Gottlieb


Here is the code that I use in my framework.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; 
like image 42
Lee Avatar answered Oct 02 '22 14:10

Lee