Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSData dataWithContentOfFile returning nil?

I have a file path, and I need to get the data of the file in NSData. I am using:

NSError *err = nil;
NSData *d = [NSData dataWithContentsOfFile:file options:nil error:&err];

NSLog(@"error: %@", err);

Error is:

 Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x34a8f0 {NSFilePath=file://localhost/var/mobile/Applications/A19223D4-0AEF-4677-8EDD-0D2CA9A7BB73/Documents/12-03-25%2022:10:48--cc.mp4, NSUnderlyingError=0x34a560 "The operation couldn’t be completed. No such file or directory"}

But the file/directory does exist because I am playing the video file and it works just fine:

MPMoviePlayerViewController *controller = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:[video filepath]]];
[self presentMoviePlayerViewControllerAnimated:controller];

What is going wrong? Thanks

like image 539
0xSina Avatar asked Sep 13 '25 17:09

0xSina


1 Answers

You are passing a URL in string format to dataWithContentsOfFile, you have to use the "file path" for the file.

In this case you have to remove "file://localhost" from the string. If the file exists, that should work.

like image 155
LuisEspinoza Avatar answered Sep 16 '25 09:09

LuisEspinoza