Friday, 18 January 2019

in iOS, in pure C, is this the way to get a local file path?

I need to end up with a cstring ... being the local path to a file in the bundle.

First,

#include <CoreFoundation/CoreFoundation.h>

(which is pure C right? .. you can't use Foundation)

and then

CFBundleRef mb = CFBundleGetMainBundle();
CFURLRef ur = CFBundleCopyResourceURL(mb, CFSTR("blah"), CFSTR("txt"), NULL);
CFStringRef imagePath = CFURLCopyFileSystemPath(ur, kCFURLPOSIXPathStyle);

CFStringEncoding encodingMethod = CFStringGetSystemEncoding();
const char *filename = CFStringGetCStringPtr(imagePath, encodingMethod);

printf( "\n we did it! .. %s \n", filename );

Assuming "blah.txt" is in your bundle, it works.

This is the only way I know to do it - seems long-winded. What's the deal?

  1. Is my code long-winded, pansy-assed, and pathetic?

  2. Is this is the correct and best idiom to get a cstring path from the filename?

(Note I do not want to make it an obj-c or obj-cpp file. Pure C.)



from in iOS, in pure C, is this the way to get a local file path?

No comments:

Post a Comment