How to get "NSURL" 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 string NSURL* url = [[NSURL alloc] initWithString:documentsDirectory];does not seem to work. urlstays nilafter the line.

+4
source share
1 answer

You need to use the file url to get the location of the resource in the file system.

[NSURL fileURLWithPath: documentsDirectory]

You can also use NSFileManagerto get the same URL.

NSArray *arr = [[NSFileManager defaultManager] URLsForDirectory: NSDocumentDirectory inDomains: NSUserDomainMask];
NSURL *documentsUrl = [arr firstObject];
+14
source

All Articles