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

5

64

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?

Nival answered 7/11, 2008 at 16:2 Comment(1)
typo: NSDocumentsDirectory ==> NSDocumentDirectoryOverlay
D
97

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.

Danuloff answered 7/11, 2008 at 16:48 Comment(2)
Cool, that's what I'll use, but why it and not the other way?Nival
You can use the other way, but if an upgrade to the OS changes the default structure, this answer is guaranteed to still work with the new layout while the first way will either fail or start re-creating legacy directories.Decolorize
V
50

Here is the code that I use in my framework.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
Viscountess answered 6/12, 2008 at 23:37 Comment(3)
Since Xcode 5, you should use [paths firstObject] instead of [paths objectAtIndex:0].Pelagic
Or "NSString *documentsDirectory = paths[0];"Overlay
Or paths.firstObject since it's a property.Grandaunt
G
16

You should consider using the FileManager methods which return URLs, which are the preferred format.

let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first

This method is intended to locate known and common directories in the system.

An array of URL objects identifying the requested directories. The directories are ordered according to the order of the domain mask constants, with items in the user domain first and items in the system domain last.

Gropius answered 11/10, 2012 at 23:34 Comment(1)
That is right. Our knowledge should be upgraded that in iOS we should use NSURL, rather than NSString (for path) to represent local file.Donar
D
1

I use this

NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *zipLocalPath = [documentPath stringByAppendingString:fileName];
Dinger answered 17/1, 2015 at 3:25 Comment(0)
Q
-1

In swift v3, I used the following snippet

var paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
Questionable answered 9/9, 2017 at 11:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.