[ACCEPTED]-How to get URL for application's document directory iPhone-iphone

Accepted answer
Score: 51

An alternative solution would be to use 3 the -[NSFileManager URLsForDirectory:inDomains:] method and fetching the path from the 2 returned NSArray:

Objective-C:

NSArray *paths = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSURL *documentsURL = [paths lastObject];

Swift:

let paths = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
let documentsURL = paths[0] as! NSURL

This 1 is supported in iOS 4.0+

Score: 43

You can get the document path via the NSSearchPathForDirectoriesInDomains foundation function as 1 follows:

NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [searchPaths objectAtIndex:0];

This will work on iOS 2+.

Score: 19

Swift 3

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

0

Score: 7

Here is the Swift 2.2 version:

let paths = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
let filePath = paths[0].URLByAppendingPathComponent("filename")

0

Score: 0

Starting iOS 16.0 you can use:

URL.documentsdirectory

source

0

More Related questions