I have this code:
NSString *logsPath = [dataDirectoryPath stringByAppendingPathComponent:@"Logs"];
Which returns:
/var/mobile/Applications/AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA/Documents/Mobile Documents/Data/Logs
However, doing this:
NSURL *logsURL = [NSURL URLWithString:logsPath];
returns a value of nil.
Any ideas as to why this might be?
Try using +fileURLWithPath:
instead.
Because +URLWithString:
expects a protocol (eg http://, https://, file://), it cannot build a URL.
On the other hand, +fileURLWithPath:
just takes the raw path, and automatically appends the file:// protocol to the path you supply.
[NSURL urlWithString:logsPath]
expects for the url to start with https:// or http://. [dataDirectoryPath stringByAppendingPathComponent:@"Logs"];
returns a path and not a URL. To fix this use [NSURL fileURLWithPath:logsPath]
. This will add file:// to the beginning of the URL making it work. Your full code will look like this:
NSString *logsPath = [dataDirectoryPath stringByAppendingPathComponent:@"Logs"];
NSURL *logsURL = [NSURL fileURLWithPath:logsPath];
Best of luck!
The actual problem is that spaces are illegal in URL paths. -fileURLWithPath: works because it URI encodes the space, not because it adds a scheme.
(lldb) po [NSURL URLWithString:@"/foo bar"]
nil
(lldb) po [NSURL URLWithString:@"/foo-bar"]
/foo-bar
(lldb) po [NSURL fileURLWithPath:@"/foo bar"]
file://localhost/foo%20bar
(lldb) po [NSURL URLWithString:@"/foo%20bar"]
/foo%20bar
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.