Question

I want to parse the location of my Documents Directory to NSUrl from NSString I have.

when my console NSLog(stringURL)

i get: "/var/mobile/Applications/2B35D06D-6E4A-40F2-833E-28463A946834/Library/Application Support/MyAppDirectory" as an output.

but when I do

NSURL* url = [NSURL URLWithString:stringUrl];

and NSLog(url) i get (null) as an output.

WHat am I doing wrong here?

Was it helpful?

Solution

From the apple documentation "An NSURL object initialized with URLString. If the URL string was malformed or nil, returns nil." Your stringURL isn't a correct formed url. For reference: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html#//apple_ref/occ/clm/NSURL/URLWithString:

What you actually want to use is: fileURLWithPath: isDirectory: instead.

NSURL *url = [NSURL fileURLWithString:[stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] isDirectory:YES];
NSLog(@"%@", url);

OTHER TIPS

How about:

NSURL* url = [NSURL fileURLWithString:stringUrl];
//                  ^^^^

Use fileURLWithPath instead (since you are passing a file path):

NSURL* url = [NSURL fileURLWithPath:stringUrl];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top