How to find file path in application package (NSBundle) using C?

Is there a C API for finding a file path in an application bundle?

I know this can be done in Objective-C with the following syntax.

NSString *path = [[NSBundle mainBundle] pathForResource:@"MyImage" ofType:@"bmp"]; 

Is there a corresponding function that I can call from C or C ++ code?

+6
source share
2 answers

After Mike K pointed me in the right direction , I managed to find the path to the file in the application bundle with the following code.

 // Get a reference to the main bundle CFBundleRef mainBundle = CFBundleGetMainBundle(); // Get a reference to the file URL CFURLRef imageURL = CFBundleCopyResourceURL(mainBundle, CFSTR("MyImage"), CFSTR("bmp"), NULL); // Convert the URL reference into a string reference CFStringRef imagePath = CFURLCopyFileSystemPath(imageURL, kCFURLPOSIXPathStyle); // Get the system encoding method CFStringEncoding encodingMethod = CFStringGetSystemEncoding(); // Convert the string reference into a C string const char *path = CFStringGetCStringPtr(imagePath, encodingMethod); fprintf(stderr, "File is located at %s\n", path); 

It seems to be a little longer than necessary, but at least it works!

+7
source

You can get the main package through:

 CFBundleRef mainBundle = CFBundleGetMainBundle(); 

Full details here:

http://developer.apple.com/library/mac/#documentation/CoreFOundation/Conceptual/CFBundles/AccessingaBundlesContents/AccessingaBundlesContents.html

+6
source

Source: https://habr.com/ru/post/905386/


All Articles