Search code examples
objective-c++

What is the path to the Mac OSX application Contents folder


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.


Solution

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