Integration Steps for Cocos2d-x iOS JavaScript

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 JavaScript Cocos2d-x project, do the following:

1. Download the SDK and plugin

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 these 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 JavaScript project

From the plugins/cocos2d/js folder in the downloaded SDK, take the ChocolatePlatformCocosPlugin.js file and add it as a script to your cocos2d-x project (usually, this would be in the assets/Script folder).

3. Link the plugin to your JavaScript project

Where you do this is up to you and depends on your project’s structure. You want to initialize the plugin and store it in a variable you can access:

this.chocolate = require('ChocolatePlatformCocosPlugin');

If you want to receive callbacks based on Chocolate Platform’s events, you can subscribe to them:

this.chocolate.prototype.callbackFunction = this.chocolateCallback;

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 setAdRequestUserData function:

//Set USER parameter used for better Ad targeting and higher yield (Not Compulsory)
        //Developer can pass empty string for any Param like ""
        //Param 1 : Age
        //Param 2 : BirthDate (dd/MM/yyyy)
        //Param 3 : Gender (male/female/other)
        //Param 4 : Marital Status (single/married/divorced/widowed/separated/other)
        //Param 5 : Ethinicty (example : Africans/Asian/Russians)
        //Param 6 : DMA Code (in String format)
        //Param 7 : Postal Code (in String format)
        //Param 8 : Current Postal Code (in String format)
        //Param 9 : Location latitude in string format
        //Param 10 : Location longitude in string format
        this.chocolate.prototype.setAdRequestUserData("23", "23/11/1990", "male", "single", "Asian",
                "999", "123123", "321321", "28.70", "77.10");

To provide information about the app, invoke the setAdRequestAppData function:

//Set APP parameter used better Ad targeting and higher yield (Recommended)
        //Developer can pass empty string for any Param like ""
        //Param 1 : App Name
        //Param 2 : Publisher Name
        //Param 3 : App Domain
        //Param 4 : Publisher Domain
        //Param 5 : PlayStore URL of the App
        //Param 6 : Ad Category
        this.chocolate.prototype.setAdRequestAppData("CocosDemo", "Chocolate",
                "cocos-demo.com", "chocolateplatform.com", "", "IAB1");

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

this.chocolate.prototype.setPrivacySettings(
  false, //false if not subject to gdpr. true if subject to gdpr.
  null  //gdpr consent string -- pass one if obtained consent from user
);

5. Initialize the sdk

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. In Cocos2D-X, this is usually handled in the onLoad function of the scene.


onLoad: function() {
  this.chocolate.prototype.initWithAPIKey(YOUR_API_KEY_GOES_HERE);
}

6. Load the ad units

Load Interstitial Ad

this.chocolate.prototype.loadInterstitialAd( YOUR_CHOCOLATE_API_KEY );

Load Reward Ad

this.chocolate.prototype.loadRewardAd( YOUR_CHOCOLATE_API_KEY );

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

chocolateCallback: function (adType, adEvent) {
    console.log("Publisher Event : Ad Type : " + adType + " Ad Event : " + adEvent);

    if (adEvent === ChocolatePlatformGlobal.INTERSTITIAL_AD_LOADED) {

        console.log("Interstitial Ad Loaded...");

        //Interstitial ad has been fetched, so let's show it now.
        this.chocolate.prototype.showInterstitialAd();
    }

    if (adEvent === ChocolatePlatformGlobal.REWARD_AD_LOADED) {
 
        console.log("Is Reward Ad Loaded...");

        //Reward ad has been fetched, so let's show it now.
        this.chocolate.prototype.showRewardAd("qj5ebyZ0F0vzW6yg", "Chocolate1", "coin", "30");
    }

    if (adEvent === ChocolatePlatformGlobal.REWARD_AD_COMPLETED) {
 
        console.log("Is Reward Ad Completed...");
    }

}

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.

8. Export to Xcode

Use the Cocos Creator to generate an Xcode project from the JavaScript.

9. Integrate the SDK and mediation partners

You can follow the instructions here, except for the following caveat:

  • Ignore the “Ad Targeting Setup” section. That is handled by your calls to setAdRequestAppData and setAdRequestUserData functions, as described above

10. Integrate the Cocos2d plugin

From the plugins/cocos2d folder of the downloaded SDK, add the following files to your Xcode project:

ChocolatePlatformAdRequester.h
ChocolatePlatformAdRequester.mm
js/ChocolatePlatformJSAdRequester.h
js/ChocolatePlatformJSAdRequester.m
js/ChocolatePlatformCC2DXBridge.h
js/ChocolatePlatformCC2DXBridge.mm

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.