Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a `NSURL` of the document directory?

I have the following code:

+(NSURL*) getRecordingDirectory {
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];
    NSURL* url = [[NSURL alloc] initWithString:documentsDirectory]; //<-- nil
    return url;
}

The line NSURL* url = [[NSURL alloc] initWithString:documentsDirectory]; Doesn't seem to work. url remains nil after the line.

like image 814
sinθ Avatar asked Nov 30 '13 02:11

sinθ


1 Answers

You need to use a file URL to get the location of a resource on the file system.

[NSURL fileURLWithPath: documentsDirectory]

You can also use NSFileManager to get the same URL.

NSArray *arr = [[NSFileManager defaultManager] URLsForDirectory: NSDocumentDirectory inDomains: NSUserDomainMask];
NSURL *documentsUrl = [arr firstObject];
like image 54
MJN Avatar answered Nov 14 '22 17:11

MJN