NSURL isFileURL always returns NO

2019-03-25 01:11发布

问题:

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?

回答1:

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://.