I am working on Mac OS X app which using some c files, and i have a configuration file i add it to the app resources.
My question is "What is the relative path of resources folder?"
I tried
"[MyAppName].app/Contents/Resources/config.cfg"
and it works fine only when I run my app from xCode, otherwise it's doesn't work!
I thought the app starts from "MacOS" folder, so i used this path:
"../Resources/config.cfg"
but it also didn't work :(
any help please
Using relative paths is asking for trouble. Fortunately Cocoa will give you an absolute path:
NSBundle *myBundle = [NSBundle mainBundle];
NSString *absPath= [myBundle pathForResource:#"config" ofType:#"cfg"];
Related
Newbie questions.
I am aware that during testing my iPhone app in the Simulator, not all resource files are available in the [NSBundle mainBundle]. I understand that the solution is to refer to [NSBundle bundleForClass:[self class]] rather than to the mainBundle.
Questions I have:
Is refering to [NSBundle bundleForClass:[self class]] valid when the app is deployed and is it good practice?
Is it good practice to define in the app delegate...
NSBundle *appBundle = [NSBundle bundleForClass:[self class]];
// or, for in final app
// NSBundle *appBundle = [NSBundle mainBundle];
... and use appBundle throughout the code? Otherwise there are too many places where I refer to [NSBundle mainBundle] that I would have to change to the bundleForClass:[self Class].
If I go with this appBundle definition, should I be doing anything else? Perhaps release the appBundle before the app delegate terminates?
Thank you!
Sleiman
mainBundle contains everything inside a bundle, the resources for your target (app). XCode tries to add all the assets into the bundle but sometimes misses or other times you may have inadvertently told XCode not to copy it into the bundle. It's important to remember to only put into the bundle what the app needs before release -- if you have test files, remove them -- because they make your app larger which eats up users storage and bandwidth.
To add/remove/inspect what is in the bundle go to the file/group inspector, the little folder icon at the top-left of XCode. Choose the project, the very top-most entry. Select a target if you have more than one. Click Build Phases and scroll down to Copy Bundle Resources. That is where you manage what gets copied into the bundle.
I wanted to separate my resources, nib files and localization files into a common reusable bundle. And so I created a bundle for my ios application and specified resources to be included inside the bundle using build phases, copy bundle resources. But, now if I try to load the bundle, I am not able to load the bundle. I try using [NSBundle allBundles] and the array shows only the main apps bundle.
I also tried to enumerate the directory for NSApplicationPath but again the only bundle available is my application default bundle. I wanted to learn this technique and make use of it to separate my resources. Any help and suggestions would be greatly appreciated. Thank you
[NSBundle bundleWithPath:[NSString stringWithFormat:#"%#/%#", [[NSBundle mainBundle] bundlePath], pathToYourBundleWithinTheDotAppDirectory];
Let me know how you get on.
Try something like this:
NSBundle* bundle=[NSBundle bundleWithIdentifier: #"bundle name"];
And make sure that you have selected these options when you have dragged the bundle to the project:
For projects with one bundle, I use:
// in this example, we load test.png from the bundle
NSString *pathToResource = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"png"];
What makes this so convenient is that when you have localized files, this provides the path the the file for the current user locale. This is handy since localized files are not in the main directory, but are rather in their own subfolders (for example, English localized files are in the #"en.lproj" subfolder), and calling them by name is a hassle because you need the full path. This method gets the path for you.
How can I reveal a bundle contents in Mountain Lion using [NSWorkspace selectFile:nil inFileViewerRootedAtPath:pathEndingWithDotBundle]? I mean: pathEndingWithDotBundle is a path whose basename is something like "folder.bundle". If I call this method this way, the Terminal opens up and I don't know why...
It opens Terminal because it thinks you want to launch the bundle, and that's the default application. I would open a bugreport against this, because the documentation does not say that it will open the path. It says it that it will display it in a file viewer. It would be reasonable for this to be an error (since a bundle is not logically a directory; it's just phyiscally a directory). But it makes no sense for it to do something random like try to launch another program.
That said, it's fairly easy to work around. Just select the Contents folder, which is required to be within the bundle:
[[NSWorkspace sharedWorkspace] selectFile:[pathEndingWithDotBundle stringByAppendingPathComponent:#"Contents"]
inFileViewerRootedAtPath:pathEndingWithDotBundle];
In 10.6+, you can use activateFileViewerSelectingURLs:
NSURL *URL = [NSURL fileURLWithPath:[pathEndingWithDotBundle stringByAppendingPathComponent:#"Contents"]];
[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:#[URL]];
In Xcode, the Resources folder links to the NSDocumentsFolder of the app? Or what else?
Is there a way to see the files in NSDocumentFolder without write code?
No the resource folder in Xcode does not link to the document directory, it is just imaginary folder for organization in xcode. However there you could see the content of your document directory by browsing to the folder in the simulator.
The folders you use in XCode will not be copied to your application bundle even though all their content (as well as anything else you have in your XCode project...excluding compiled resources) will be flatly copied into your Bundle Folder. If what you need is accessing the Documents folder on an iOS app or in a the User's library you can access it (by code, I'm sorry) with:
NSString *documentFolderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
And anything you need to be in there you will have to copy by code.
I have a helper application, which is within my main application's application bundle (in the Resources). I'm not sure how to get the path of the application from inside the bundle and launch it.
I'm not sure I fully understand the question. I think you're saying you have an application (let's call it PrimaryApplication.app) and inside it's Resources directory there is an application you need to launch (let's call it Helper.app). In that case you use NSBundle's -bundlePath to get the path to the currently running application, then you append the path to your helper from there. You can use NSWorkspace to launch the application once you know the path to it.
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *helperAppPath = [[mainBundle bundlePath]
stringByAppendingString:#"/Contents/Resources/Helper.app"];
[[NSWorkspace sharedWorkspace] launchApplication:helperAppPath];
In Core Foundation, CFBundleCopyResourceURL should get you the application's url.
In Cocoa, NSBundle has equivalent pathForResource:ofType: and URLForResource:withExtension: methods.