What is the path to the Mac OSX application Contents folder - objective-c++

I am writing an Objective-c++ application and I am trying to reach the applications Contents folder.
Let's pretend my apps name is MyApp.app. If I run my application from Xcode then I can reach the Contents folder like this:
std::fstream file;
file.open("MyApp.app/Contents/some_text_file.txt");
if(file.is_open()) {
...
}
else {
...
}
But If I run my built application without Xcode, then file.is_open() fails.

[[NSBundle mainBundle] bundlePath] will give you the path of the MyApp.app directory. You can then add Contents to that path.
Alternatively, [[NSBundle mainBundle] resourcePath] will give you the path to the MyApp.app/Contents/Resources directory. You can then remove the subdirectory from that path; see Working with paths from [[NSBundle mainBundle] resourcePath]

Use NSBundle class to get informations on your application bundle.
[[NSBundle mainBundle] bundlePath] give you full path to MyApp.app.

Related

App don't see files

This all started when I changed my_sound.mp3 to another sound with the same name in 'Supporting Files' folder, but after compiling app was still playing the old one. After that I've done a lot of manipulations.
What I've tried:
Delete app from iPhone and from Simulator
Delete all from Library/Developer/Xcode/DerivedData
Product > Clean in Xcode
Now my app don't see my_sound.mp3 at all...
How I check:
NSString* path = [[NSBundle mainBundle] pathForResource:#"my_sound" ofType:#"mp3"];
if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
NSLog(#"No file");
}
Files created long time ago in 'Supporting Files' are available.
"Is my_sound.mp3 listed with the correct name in the "Copy Bundle Resources" section of the target's Build Phases?" – Phillip Mills Aug 4 at 19:00

iPhone: Fontconfig error: Cannot load default config file

I have the following problem:
I included a static library of FontConfig libfontconfig.a into my iPhone project.
The application is building and i can run it on the device but when I do a call to a method that needs some functions of this library the application crashes with the following error
Fontconfig error: Cannot load default config file
terminate called throwing an exception
I read that this error normally occurs when FontConfig cannot find the fonts.conf file that is normally located in /etc/fonts.
But I cannot move that file to this directory of the iPhone. Has anyone a solution? Just copying the file to the application bundle did not help, as well.
I would appreciate any help.
I finally found a way to solve this problem. I set an Environment Variable FONTCONFIG_FILE and FONTCONFIG_PATH that define the locations of the fonts.conf file and the path that was before /etc/fonts. So I can add the fonts directory to the project and define the path #runtime.
The Environment Variables have to be set in your code before you call a method that needs a functionality of FontConfig. I set it after starting my Application in the AppDelegate.
Here is the code I added:
//Get the directory of your Application
NSString *bundlePath = [[NSBundle mainBundle] resourcePath];
//Create a String with the location of your fonts.conf file
NSString *fontconfigFile= [bundlePath stringByAppendingString:
[NSString stringWithFormat:#"/fonts/fonts.conf"]];
//Create a String with the path of the FontConfig configuration path
NSString *fontconfigPath= [bundlePath stringByAppendingString:
[NSString stringWithFormat:#"/fonts"]];
//Set the Environment Variables
setenv("FONTCONFIG_FILE", [fontconfigFile UTF8String], 0);
setenv("FONTCONFIG_PATH", [fontconfigPath UTF8String], 0);
In a second step I had then to modify my fonts.conf file. I added the fonts directory to FC/fonts and the cache directory to FC/cache of my project and adjusted the fonts.conf file on two parts.
I changed the first part:
<!-- Font directory list -->
<dir>/usr/share/fonts</dir>
<dir>/usr/X11R6/lib/X11/fonts</dir>
<dir>~/.fonts</dir>
to:
<!-- Font directory list -->
<dir>FC/fonts</dir>
and the second part:
<!-- Font cache directory list -->
<cachedir>/opt/local/fc1407cd/var/cache/fontconfig</cachedir>
<cachedir>~/.fontconfig</cachedir>
to:
<!-- Font cache directory list -->
<cachedir>FC/cache</cachedir>
Then it did not crash anymore.

Resource Folder Xcode 4, how to delete files from package

I am using Xcode 4. I have copied
several images to main folder, I
have built project, next I have
deleted all images. When I list
resources path by NSLog, I can see
that there are still in this
package. I've deleted build folder,
clean etc. What should I do to
delete this files from my package
And second question. Why when I copy
an entire folder, and list files and
directories by:
NSFileManager *fm = [[NSFileManager alloc] init];
NSString *string = [[NSBundle mainBundle]resourcePath];
for(NSString *name in [fm contentsOfDirectoryAtPath:string error:nil])
{
NSLog(#"%#", name);
}
In console a can only see files from this folder.
Have you deleted the project from simulator ? You can do it in the Simulator app or deleting the project folder at ~/Library/Application\ Support/iPhone\ Simulator/

loadable bundle for plists

I have a main project and a static library project inside the main project. I want few plists in my static library project. Since there is no resource directory it is not possible to add plists directly, What is did is this.
1.Added a loadable bundle to my static library project
2.Added few plists to loadable bundle
3.This loadable bundle is made as a dependency to my static library project
4.Added this loadable bundle as a dependency to my main project as well
Am i going on the right path ? when i fired the build and used show contents to see the contents of the app i was able to see the .bundle file inside the .app file. Right now i want to know how to access this bundle . .
i used this
NSBundle *staticlink = [NSBundle mainBundle] pathForResource:#"MyBundle" ofType:#"bundle"];
[staticlink load];
NSLog(#"%#",staticlink);
console said this . .
. . . /mainproject.app/MyBundle.bundle <not yet loaded>
What is missing ? what should i do load MyBundle ?
-pathFoResource:ofType: returns a NSString object which will have the path to the bundle.
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:#"MyBundle" ofType:#"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
[bundle load];

how to know the path of file in iphone sdk?

i want to connect database to iphone sdk but it is taking the default path inside the system folders but not in the app bundle ?how can i change the file path from that system directory to file in my app bundle?
This should retrive your app's document folder path :
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];