Differences in accessing resources between Simulator and Device? - iphone

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?

[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]

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

Related

Why there is a issue editing PLIST file only on iPhone 4?

I'm actually coding and testing an app on a iPhone 4s, and my PLIST editor works like a charm.
Then I wanted to try it on an iPhone 4, and I constated that the PLIST file is never edited.
Here my code for writing my array into a file :
- (BOOL)writeArrayToPlist:(NSArray*)plistArray
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"DataList" ofType:#"plist"];
[[NSFileManager defaultManager] removeItemAtPath:filePath error:NULL];
return [plistArray writeToFile:filePath atomically:YES];
}
Am i missing something ? thanks !

Get name of Xcode Project in actual code

So you know the part of the project that is "MyNameGoesHere.app" or "MyNameGoesHere.xcodeproj" - is there a way to get the MyNameGoesHere part via objective-c code?
I can get all kind of device info from UIDevice messages but I can't figure out how to get project/app name.
CFBundleDisplayName doesn't work anymore. Use:
NSString *bundleName = [[NSBundle mainBundle]
objectForInfoDictionaryKey:#"CFBundleName"];
You're probably looking at the bundle's InfoDictionary. You can get the app's name via the following code:
NSDictionary *info = [[NSBundle mainBundle] infoDictionary];
NSString *bundleName = [NSString stringWithFormat:#"%#", [info objectForKey:#"CFBundleDisplayName"]];

Application crashes at [[NSBundle mainBundle] pathForResource

My Application is crashes in the device at this point,
[[NSBundle mainBundle] pathForResource
I am giving the path for the pdf here . but at the execution time it is showing that it is not getting path there . and that may be the reason for crashing.
however it is running perfectly on simulator .
i am not able to figure out why is this happening
here is that code
NSString *path = [[NSBundle mainBundle] pathForResource:[myArray objectAtIndex:0] ofType:[myArray objectAtIndex:1]];
NSLog(#"array elemt :%#", [myArray objectAtIndex:0]);
NSLog(#"array elemt 1 :%#", [myArray objectAtIndex:1]);
NSLog(#"path is :%#",path);
NSLog(#"responds to selector mainBundle=%#",[NSBundle respondsToSelector:#selector(mainBundle)]?#"YES""NO");
NSURL *targetURL = [NSURL fileURLWithPath:path];
in this log of the path - when i use device it is showing me nil and when i use simulator it is showing me the path . and it is showing me that is is crashing on NSURL line
I've found that when something like this works on the simulator, but not on the device, it's likely a problem with the case of the string. Your device is case sensitive but not your computer. Check your string of the file name or the type.

How should I specify the path for an sqlite db in an iPhone project?

After adding it as a resource, the database file itself is in the project root.
I've only been able to open it by specifying the full path as OS X sees it, i.e., "/Users/Louis/Documents/Test Project/test.db".
But of course there is no such path on an iPhone.
I think I should define the path as "application root/test.db" but I don't know how, or if that would even work anywhere else besides my development machine.
Thanks for any ideas.
To get the path of the file you've added in XCode you would use pathForResource:ofType: with your mainBundle.
NSString *path = [[NSBundle mainBundle] pathForResource:#"yourDb" ofType:#"sqlite"];
But you can't change files in the mainBundle. So you have to copy it to another location. For example to the library of your app.
You could do it like this:
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
NSString *targetPath = [libraryPath stringByAppendingPathComponent:#"yourDB.sqlite"];
if (![[NSFileManager defaultManager] fileExistsAtPath:targetPath]) {
// database doesn't exist in your library path... copy it from the bundle
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:#"yourDb" ofType:#"sqlite"];
NSError *error = nil;
if (![[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:targetPath error:&error]) {
NSLog(#"Error: %#", error);
}
}
Don't just use the SQLite API, use this amazing wrapper called FMDB: https://github.com/ccgus/fmdb
Getting Paths to Standard Application Directories

How to access files in interior of an iPhone App?

I need in a iPhone app to access files that the app is build with(.plist etc). There's an hardcoded way to do this:
NSString *appDir = [[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
objectAtIndex:0]
stringByDeletingLastPathComponent]
stringByAppendingPathComponent:appFolder];
where appFolder is the name of folder app, like "test.app". After the appDir is known, to access files is simple.
Is there any other, not-hardcoded way to have access to files form the app?
Thanks in Advance!
NSString* pathToFile =
[[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:#"myFile.plist"];
// or, even better (handling localization):
NSString* pathToFile = [[NSBundle mainBundle] pathForResource:#"myFile"
ofType:#"plist"];
Your app folder is the "main bundle". So you can use NSBundle methods such as
NSString* path = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"plist"];