Integration Steps for Cocos2d-x C++ iOS

The Chocolate Platform iOS SDK Cocos2d-x plugin supports the following ad units:

  • Full Screen Interstitial
  • Rewarded Video

In order to display Chocolate Ads in your C++ Cocos2d-x project, do the following:

1. Download the SDK

iOS projects generated by the Cocos2d-x platform do not interoperate with CocoaPods correctly. You will have to integrate the ChocolatePlatform SDK manually. Download

After unzipping, you will have a CHOCOLATE_IOS_SDK_{VERSION_NUMBER} folder with three subfolders:

  • An SDK folder containing the ChocolatePlatform libraries
  • A Mediation folder containing libraries from Chocolate Platform’s mediation partners
  • A plugins folder containing a subfolder cocos2d, which contains the cocos2d-x plugin

2. Add the plugin to your C++ project

From the plugins/cocos2d folder in the downloaded SDK, add the following files to your project (usually, this means adding them to your project’s Classes directory:


3. Use the plugin

Where you do this is up to you and depends on your project’s structure. Your C++ code will interface with the Chocolate Platform SDK’s via the code in ChocolatePlatformCC2DXBridge. If you wish to receive callbacks, you need to subscribe to them by passing in an instance of ChocolatePlatformInterstitialCallbackReceiver or ChocolatePlatformRewardCallbackReceiver. These are pure abstract classes defined in ChocolatePlatformCC2DXBridge.h, so it is safe to use them as follows:

#include "cocos2d.h"
#include "ChocolatePlatformCC2DXBridge.h"

class YourScene : public cocos2d::Scene, 
                  public ChocolatePlatformInterstitialCallbackReceiver, 
                  public ChocolatePlatformRewardCallbackReceiver

Then, within that class, you can subscribe to the callbacks:


If you wish to show only interstitial or only reward callbacks, you can subclass only one of the CallbackReceiver classes, and subscribe to only one set of callbacks.

4. Configure the app with additional information

You can optionally provide Chocolate Platform’s SDK with information about the app’s user, the user’s location, or the app itself for better ad targeting.

To provide information about the app’s user and the user’s location, invoke the SetAdRequestUserParams function from ChocolatePlatformCC2DXBridge:

                                     "23",          //age
                                     "23/11/1990",  //birthday
                                     "male",        //gender
                                     "single",      //marital status
                                     "Asian",       //ethnicity
                                     "999",         //dma code
                                     "123123",      //postal code
                                     "321321",      //current postal
                                     "0.0",         //latitude
                                     "0.0"          //longitude

To provide information about the app, invoke the SetAdRequestAppParams function from ChocolatePlatformCC2DXBridge:

                                  "ChocolateAds",          //app name
                                  "Chocolate",             //publisher name
                                  "",      //app domain
                                  "", //publisher domain
                                  "",  //app store url
                                  "Utilities"              //app category

To aid with compliance to the EU’s GDPR regulation that became effective on May 25, 2018, configure the app as follows:

  true, //gdpr applies
  null  //gdpr consent string -- pass one if obtained consent from user

Ad delivery can be improved by initializing the SDK early. It’s a good idea to make this call once, soon after the app launch. This will make it less likely that the first request for an ad will time out due to a lengthy initialization process.


5. Load the ad units

Load Interstitial Ad

loadInterstitialAd( YOUR_CHOCOLATE_API_KEY );

Load Reward Ad


6. Implement the ad callbacks and show the ads on successful loading

void YourScene::interstitialAdLoaded(){

void YourScene::interstitialAdFailed(){
    CCLOG("%s", "interstitial ad failed);

void YourScene::interstitialAdShown(){}

void YourScene::interstitialAdClicked(){}

void YourScene::interstitialAdDismissed(){}

#pragma mark - Reward ad callback method

void YourScene::rewardedAdLoaded(){
                 30,                //reward amount
                 "coin",            //reward name
                 "Chocolate1",      //reward user id
                 "XNydpzNLIj2pBRM8" //secret key

void YourScene::rewardedAdFailed(){
    CCLOG("%s", "reward ad failed);

void YourScene::rewardedAdShown(){}

void YourScene::rewardedAdDismissed(){}

void YourScene::rewardedAdFinished(int rewardAmount, const char *rewardName){
    CCLOG("%d of %s rewarded", rewardAmount, rewardName);

Note: implementation of callbacks is optional, and you can choose to take another path, such as additional UI before the user sees the ad. However, calling the show methods before the load completes, either on interstitial or rewarded ad units, will produce no result.

7. Integrate the SDK and mediation partners

After opening the Xcode project generated by the cocos tool, you can follow the instructions here, with the following caveats:

  1. Ignore the “Ad Targeting Setup” section. That is handled by your calls to setAdRequestAppData and setAdRequestUserData functions, as described above
  2. You might have to add GameController.framework to the project to get the cocos2d library to compile once the -ObjC flag is set in the app build settings.

Run the app from Xcode and see ads.


How to turn on ads on your ad unit

Please send us your sample app with the integrated code. We will help you validate the SDK setup and turn on the demand.