views:

44

answers:

2

Is there some difference between the way that bundle resources can be accessed on the iPhone simulator versus a device (in my case, an iPad)? I am able to access a file on the former, but not the latter.

NSString *filePath = [NSString stringWithFormat:@"%@%@",[[NSBundle mainBundle] bundlePath], @"/AppResources/html/pages/quickHelp.html"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
// fileExists == YES in the debugger on both the simulator and the device

NSString *path = [NSString stringWithFormat:@"AppResources/html/pages/%@", contentsFileName];
NSString *pathForURL = [[NSBundle mainBundle] pathForResource:path ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:pathForURL isDirectory:NO];

The code above works fine in the simulator, but on the device pathForResource:path returns nil, so the last line throws a 'nil string parameter' exception.

Am I missing something obvious?

edit: Of course, in the above @"quickHelp" is being passed in the contentsFileName var.

edit2: if it makes any difference, in my build settings "Base SDK" is set to "iPhone Device 4.0", and "iPhone OS Deployment Target" is set to "iPhone OS 3.1.3". Any other settings that might have an influence?

A: 

[ignore]
I notice a leading '/' on your *filePath but none exists on *path (before AppResources)
[/ignore]

[edit]
[[NSBundle mainBundle] pathForResource:@"quickHelp" ofType:@"html" inDirectory:@"AppResources/html/pages"]]
[/edit]

KevinDTimm
pathForResource adds that, no? It does in the simulator, at least...I had to add it in *filePath just so that I could create the entire path by hand.
Tony
since you're looking for a file called quickHelp.html, shouldn't you just put ... pathForResource:@"quickHelp" ofType:@"html"]? (since it's going to search down your apps path anyway)
KevinDTimm
No, I don't think that it will recursively search through subdirectories. I did try it anyway, hard-coding as pathForResource:@"quickHelp" ofType:@"html", but that causes the code to fail in both the simulator and the device.
Tony
weird stuff (not going to help you much) but see http://www.iphonedevforums.com/forum/sdk-coding-help/2121-nsbundles-pathforresource-returns-null.html
KevinDTimm
however, this looks promising : http://www.iphonedevsdk.com/forum/iphone-sdk-development/31256-nsbundle-pathforresource-indirectory-gives-nil.html
KevinDTimm
Aha! Your edited comment did the trick (using inDirectory:). Funny that that's required in this case on the device. To make things even weirder, I have other resources (AIFF files) that are accessed the same way as I had above with no problem... Gremlins, I suppose. Thank you for the help!
Tony
A: 

I believe the filesystem on the iPhone is case-sensitive. Check your cases with the actual files.

Jeff Kelley
I'm pretty sure that if it were a case problem then fileExists wouldn't return YES, right?
Tony