Recently i created a library called EasyNDK (https://github.com/aajiwani/EasyNDK-for-cocos2dx). I was very happy when my code got live, to me it made complete sense, but i guess that wasn’t the case for all the developers out there. Integrating their own SDKs or using 3rd party SDKs wasn’t easy using my code as it seems to be. So, i thought to create a simple tutorial for the most common and pretty easy SDK that is available in both iOS and Android, known as Chartboost (https://www.chartboost.com).
Lets see how easy is the process to integrate it using EasyNDK to have a single code base in C++ and integrate it with Android(Java) and iOS(Objective-C).
1.) Go to Chart boost’s website to create an account or to already use one. (https://www.chartboost.com/)
2.) Download their SDK for the required platform
- https://help.chartboost.com/downloads/ios – iOS
- https://help.chartboost.com/downloads/android – Android
3.) Create the required application with settings under your account from your dashboard. (https://dashboard.chartboost.com/)
4.) For EasyNDK to work as expected, lets make a receiver held responsible from the native side (Java / Obj-C) to receive messages from C++ and execute them on the native platforms. Here are the steps to be taken.
For iOS, in RootViewController.mm
Add this line of code in init method of your RootViewController
For Android, in your MainActivity.java
Add this line of code in onCreate method of your MainActivity
5.) Lets integrate the SDKs with the help of guide available on Chart boost’s site. Please note do not write the Interstitial on the applicationDelegate in iOS or in onCreate method of your activity in Android. I will later show how we will write the showInterstitial method in both platforms. Else than that follow every step that is written under the help sections.
- https://help.chartboost.com/documentation/ios – iOS, Note you can find the ApplicationDelegate in ios folder with a file name AppController.mm
- https://help.chartboost.com/documentation/android – Android
6.) This is the time where our EasyNDK will work, for simplicity in the attached example i am working with the basic hello world project of Cocos2dx, i will simply add a pin button on the left hand side of the close button that is already provided in the example. On tapping it, the showInterstitial method will start working, lets see how.
Step 1.) In your class file (C++) where you want to see the chart boost button, add a button and attach a callback function to it, for here i am doing it in HelloWorldScene.cpp
using namespace std;
Adding a button to the menu in the scene
// Chartboost button to load interstitial
CCMenuItemImage *pChartBoostItem = CCMenuItemImage::create(
// Adjust Chartboost button
pChartBoostItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 80, 20) );
// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, pChartBoostItem, NULL);
Adding the call back that will respond to the button click and will forward message to the native language accordingly, using EasyNDK helper.
void HelloWorld::chartboostInterstitialCallback(CCObject* pSender)
// If you want to load interstitial in your desired platform
// Simply message the platform to do it for you
// Like this
That is all from the C++ side, we can now successfully call the Interstitial method from any platform using the same code. Lets implement the handler on both the platform and see the magic happening.
Step 2.)Adding the message handler on both the platforms, using only the ones that are required. Be sure to name the methods(Java) or messages(Objective-C) to be of the same string as you passed from C++. For here i have passed LoadInterstitial as the string from C++ hence i will implement the method with the same name.
For iOS, in RootViewController file, as we have attached the receiver to be RootViewController, add this code.
- (void) LoadInterstitial:(NSObject *)prms
// Show an interstitial
[[Chartboost sharedChartboost] showInterstitial];
For Android, in MainActivity file, as we have attached the receiver to be MainActivity, add this code.
public void LoadInterstitial(JSONObject prms)
// Show an interstitial
7.) This is it, we have successfully integrated Chart boost SDK on both Android and iOS with a single code base in C++. Similarly you can connect any SDK you like that is already available on the native platform to work with Cocos2dx.
For the ease and saving time, i am adding sample projects for both iOS and Android with Chart boost already included, so that you can download and test the application if you haven’t understood from the above tutorial. Here are the links to the required downloads.
Thanks for reading the post.