Outdated Docs > EasyNDK > How to work on iOS - C++Objective-C

How to work on iOS - C*+ and Objective-C
h2. Purpose.
On a button click from Cocos2dx Scene, i have to send a message to Objective-C to show a popup on native platform. Lets find how to achieve it.
h2. Includes
You need to make several includes to your project, i have created an online repo that has been divided with respect to category
of environments. Please make sure you include all the files for C*+ and Objective-C. Here is the link to the online repo :


From C*+

<pre><code class="cpp">
#include “NDKHelper.h”
// The button click method of Cocos2dx
void HelloWorld::menuCloseCallback
// Register a selector in a global space
// So that when our native environment will call the method with the string
// It can respond to the selector
// Note : Group name is there for ease of removing the selectors
// Making parameters for message to be passed to native language
// For the ease of use, i am sending the method to be called name from C*+
CCDictionary* prms = CCDictionary::create();
prms->setObject(CCString::create(“SampleSelector”), “to_be_called”);

// Finally call the native method in current environment
SendMessageWithParams(string(“SampleSelector”), prms);

// A selector that will respond to us, when native language will call it
void HelloWorld::SampleSelector(CCNode sender, voiddata)
CCLog(“Called from native environment”);

// Destructor to remove all the selectors which are grouped as HelloWorldSelectors
// Remove the associated selector group from the global space,
// Because we are destroying this instance


From Objective-C

// Using a UIViewController init method to attach a receiver for messages from C++
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization

        // Tell NDKHelper that RootViewController will respond to messages
        // Coming from C++
        [IOSNDKHelper SetNDKReciever:self];
    return self;

// Implement the selector to be called for a message from C++
// Be sure to name the selector to be of the same string as you will pass from C++
// Like we passed "SampleSelector" from C++, that is why created this selector
- (void) SampleSelector:(NSObject *)prms
    NSLog(@"purchase something called");
    NSDictionary *parameters = (NSDictionary*)prms;
    NSLog(@"Passed params are : %@", parameters);

    // Fetching the name of the method to be called from Native to C++
    // For a ease of use, i have passed the name of method from C++
    NSString* CPPFunctionToBeCalled = (NSString*)[parameters objectForKey:@"to_be_called"];

    // Show a bogus pop up here
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Hello World!"
                                                      message:@"This is a sample popup on iOS"
    [message show];

    // Send C++ a message with paramerts
    // C++ will recieve this message, only if the selector list will have a method
    // with the string we are passing
    [IOSNDKHelper SendMessage:CPPFunctionToBeCalled WithParameters:nil];


For connecting others SDK’s you can simply follow Objective-C guides of those SDKs and respectively implement the message passing mechanism to call
them from Cocos2dx, be it any of the SDK. I have implemented GameCenter, Flurry and many other SDKs using this method.

A sample project with complete source is available online, here is the link :
Sample iOS Project

Enjoy programming :)

Sign up for our newsletter to keep up with the latest developments, releases and updates for Cocos2d-x.