NSBundle mainBundle pathForResource (maybe not the same problem)

nycynik picture nycynik · Aug 3, 2011 · Viewed 10k times · Source

Problem : [NSBundle mainBundle] pathForResource returns null (0x0)

I read a lot of posts on this topic, here is what I found:

  1. Make sure the file you are trying to get a path to, is in the project. To check, look in the project file list for the file, if it is not there, drag and drop it in.

  2. Check that the file your trying to load is being copied to the app. To do this, click on project under project files (blue bar with project name in it->Click on the target->Click on Build Phases->Click to expand the "copy Bundle Resources", and make sure your file is in it. If it is not, click the plus, and add it.

  3. Make sure the case matches exactly. (aka - The simulator will work/the app will not problem) Make sure the case matches exactly, otherwise it will return nothing. To fix this, just rename it in the project, or use the right case in the source.

  4. Project may be missing the media framework. To fix this click on your project menu -> click on the target -> click to expand "Link Binary With Libraries". Now select MediaPlayer.framework and build the code.

  5. If all else fails, clean the project, and try again.

All this is checked/fixed, but not working. I had this working fine, and then I changed the video that I am showing in the app. Since i changed the file, it stopped working.

NSString *path = [[NSBundle mainBundle] pathForResource:@"multiplying" ofType:@"m4v"];

Q: why is it null? The file is case matched/typed correctly and is about 10 megs. (is the file too large?) Works fine in the simulator, and I can get the path to an image, a line above this one, with no error.

Answer

blackhawk4152 picture blackhawk4152 · Aug 3, 2011

You say it's working in simulator and not working on device? May be you should try to copy the file at first launch to one of the application sandbox folders and access it like this:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pathForTheFile= [documentsDirectory stringByAppendingPathComponent:     @"multiplying.m4v"];