NSURL isFileURL always returns NO
Asked Answered
I

1

17

Can someone explain to me why the following code:

NSString* filePathString = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

NSLog(@"%i", [[NSURL URLWithString:filePathString] isFileURL]);
NSLog(@"%@", filePathString);

outputs:

0

"/var/mobile/Applications/28ADFC19-874C-4304-94B5-F6441CAE9FAD/Documents"

This means that this url isn't a file URL. Obviously, it is.


Background:

I'm trying to use an AVCaptureMovieFileOutput to write a movie recording to a file, but the file url I'm proving gives the error:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[AVCaptureMovieFileOutput startRecordingToOutputFileURL:recordingDelegate:] - Cannot record to URL /var/mobile/Applications/28ADFC19-874C-4304-94B5-F6441CAE9FAD/Documents/media/CrKNjNhe9so2LRnD9iHK.mov because it is not a file URL.'

That looks like a file URL to me, just like the original example. What gives?

Isodynamic answered 26/11, 2012 at 0:40 Comment(0)
F
39

If you want to create an NSURL from a file path, you must use NSURL fileURLWithPath:. The use of URLWithString assumes the string is a non-file URL.

An NSURL is a file URL only if it begins with file://.

Fingerbreadth answered 26/11, 2012 at 0:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.