Using native Cocoa frameworks with OF

I’m currently working on a project that will run on mac. In it, I need to find the location of the device that program is currently running on. I have used Apple Cocoa APIs before, and if possible, I’d like to use the CoreLocation to find the device’s location. My question is, how do I interface with this API or any other Cocoa API through OF and C++?

If it is not feasible to find the location through CoreLocation, is there another way to get the user’s location with OF or an open-source library?

You can use Objective-C++ to bridge C++ code (OF) and Objective-C code (Cocoa stuff). Objective-C++ is a combination of Objective-C and C++ where you can access the features of both languages in the same source file. Objective-C++ files have a file extension of “.mm” (rather than .cpp or .m).

What I would do is create a header file with C++ functions/classes that you can call from your OF C++ code. Then create a corresponding .mm file and implement those functions/classes using Cocoa frameworks. Things can get tricky of you need to have data members in a C++ class that are actually instances of Objective-C classes. Then you must hide the Objective-C things from the header file, since the header file must compile when included from C++ code, which doesn’t understand about Objective-C classes.

For example, in the header file:

void CallSomeCocoaFunction();

and in the .mm file:

#import <AppKit/AppKit.h>

void CallSomeCocoaFunction() {
   // Do Cocoa stuff here

I don’t know how CoreLocation works, so I can’t say much about your specific problem.

Hi @pappis, thanks for the reply.

It seems like that would be a lot of work for a very simple use case, as all I need is the device’s latitude and longitude, not even to a moderate level of accuracy.
Is there another way I can get the device’s location, perhaps using the IP address?

Yes, mixing C++ and Objective-C has a bit of overhead/pain involved.

Like I wrote above, I don’t know much about CoreLocation, or what alternatives are available. I found this on
but unfortunately, it seems to require you to change your source files to Objective-C++ (.mm), at least for all source files that needs to include ofxCoreLocation.h. I think it could be rewritten so that the Objective-C class that is now in the header file is hidden inside the .mm file, but it’s tricky.

Sorry I can’t be of more help.