iOS Cocos2D-X Plugin

Getting started with the Adcash Cocos2d-x Plugin

Supported platforms:

  • Android
  • iOS

The plugin features include:

  • Support for Banner Ads
  • Support for Interstitial Ads
  • Banner positions(top/bottom)
  • Banner ad events listeners
  • A sample project to demonstrate plugin integration

And the requirements are:

  • Cocos2d
  • An ad unit ID
  • To deploy on Android:
    • Adcash Android SDK library as .JAR or as .AAR
    • Google Play services 4.0 or higher
    • Google Support Library v4
    • Android SDK
    • Android NDK r9d (other versions proved problematic and we do not recommend them)
    • Apache Ant
    • gawk
    • Linux specific:
      • Bash
      • Python
    • Mac specific:
      • Command Line Tools for Xcode
  • To deploy on iOS:

How to use

Download the plugin from our Github repo:

git clone https://github.com/adcash/adcash-cocos2dx-plugin.git ~/Projects

Let us assume you have a root directory called Projects. Create a new Cocos2d-x project:

cocos new MyGame -p com.MyCompany.MyGame -l cpp -d ~/Projects

Integrate the plugin

Copy the ~/adcash-cocos2dx-plugin/adcash folder (from the cloned repo) into the ~/Projects/MyGame/cocos2d/plugin/plugins folder (your app project):

cp -r ~/Projects/adcash-cocos2dx-plugin/adcash ~/Projects/MyGame/cocos2d/plugin/plugins/
  • Android specific

    1. Run the ~/Projects/MyGame/cocos2d/plugin/tools/publish.sh script.If you want to skip the other plugins and publish only the one you are using you can modify the line in cocos2d/plugin/tools/config.sh file:
      // from:
      ALL_PLUGINS=(`cd "${script_path}/../plugins" && find -L . -maxdepth 1 -type d`)
      // to:
      ALL_PLUGINS=("adcash")
      

      Follow the instructions – you will be asked to enter the paths to your NDK, SDK and Ant.

      In case your build is unsuccessful with the message: Unable to resolve project target 'android-7', make sure to edit the line with the target version of your Android version in your cocos2d/plugin/protocols/proj.android/project.properties:

      // from:
      target=android-7
      // to:
      target=android-<HERE> // Input the version you want to target and have available
      

      And then run the script again. When the build is successful you can continue.

    2. Run the ~/Projects/MyGame/cocos2d/plugin/tools/gameDevGuide.sh script.You will be asked to input the full path to your project. That would be like the following: ~/Projects/MyGame/proj.android. Click Next.

      Then you will be asked to select/check the plugin you need. Check adcash and then click Finish and finally Close.

    3. In ~/Projects/MyGame/proj.android/jni/Android.mk modify:
      // from:
      $(call import-module,protocols/android)
      // to:
      $(call import-module,plugin/publish/protocols/android)
      
    4. In the ~/Projects/MyGame/proj.android/jni/hellocpp/main.cpp file add:
      // header
      #include "PluginJniHelper.h"
      ...
      // cocos_android_app_init
      JavaVM* vm;
      env->GetJavaVM(&vm);
      PluginJniHelper::setJavaVM(vm);
      
    5. In ~/Projects/MyGame/proj.android/project.properties after the first reference add:
      android.library.reference.2=../cocos2d/plugin/protocols/proj.android
      android.library.reference.3=../cocos2d/plugin/plugins/adcash/proj.android
      
    6. Run the following command in the terminal:
      cocos run -p android
      

      In case your build Android target level is too low to accept configurations other than the basic ones, you will get an error stating:

      AndroidManifest.xml:19: error: Error: 
           String types not allowed (at 'configChanges' with value 'keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize').
      

      Thus you will need to edit the following two files:

      • ~/Projects/MyGame/plugin/plugins/adcash/proj.android/ForManifest.xml
      • ~/Projects/MyGame/proj.android/AndroidManifest.xml

      Inside modify:

      // from:
      <activity android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
               android:name="com.adcash.mobileads.AdcashInterstitialActivity" />
      // to:
      <activity android:configChanges="keyboard|keyboardHidden|orientation"
               android:name="com.adcash.mobileads.AdcashInterstitialActivity" />
      

      Now run the command again.

      Keep in mind that cocos run -p android operation is very lengthy as it builds and runs your whole project. On a succesful outcome you should be able to see your app running.

  • iOS specific

    1. Open ~/Projects/MyGame/proj.ios_mac/MyGame.xcodeproj.
    2. Drag & drop ~/Projects/MyGame/cocos2d/plugin/plugins/adcash/proj.ios/AdcashSDK.framework to Project
    3. Drag & drop ~/Projects/MyGame/cocos2d/plugin/protocols/proj.ios/PluginProtocol.xcodeproj to Project
    4. Drag & drop ~/Projects/MyGame/cocos2d/plugin/plugins/adcash/proj.ios/adcash-cocos2dx-plugin.xcodeproj to Project
    5. Add AdcashSDK.framework to “Link Binary With Libraries”.
    6. Add AdcashCocos2dxPlugin.framework to “Link Binary With Libraries”.
    7. Add libPluginProtocol.a to “Link Binary With Libraries”.
    8. Add MediaPlayer.framework to “Link Binary With Libraries”.
    9. Add GameController.framework to “Link Binary With Libraries”.
    10. Add $(SRCROOT)/../cocos2d/plugin/protocols/include to “Header Search Paths”.
    11. Add -ObjC to “Other Linker Flags”.

Configure the plugin

Add this to your HelloWorldScene.h:

#include "ProtocolAds.h"
...
private:
       cocos2d::plugin::ProtocolAds *_adcashSDK = NULL;

Add this to your HelloWorldScene.cpp:

#include "PluginManager.h"
using namespace cocos2d::plugin;
...
bool HelloWorld::init()
{
...
_adcashSDK = dynamic_cast<ProtocolAds*>(PluginManager::getInstance()->loadPlugin("AdcashSDK"));

// Configure plugin
TAdsDeveloperInfo devInfo = TAdsDeveloperInfo();
devInfo["bannerZoneId"] = "<YOUR_BANNER_ZONE_ID_HERE>";
devInfo["interstitialZoneId"] = "<YOUR_INTERSTITIAL_ZONE_ID_HERE>";
_adcashSDK->configDeveloperInfo(devInfo);
...

Add this snippet to your HelloWorldScene.cpp:

TAdsInfo info = TAdsInfo();
info["adType"] = "1"; // 1 for Banner
info["adSize"] = "0"; // 0 is Smart banner
_adcashSDK->showAds(info, ProtocolAds::AdsPos::kPosBottom);

Interstitial

Add this snippet to your HelloWorldScene.cpp:

TAdsInfo info = TAdsInfo();
info["adType"] = "2"; // 2 for Interstitial
_adcashSDK->showAds(info, ProtocolAds::AdsPos::kPosBottom); 
// Position for Interstitial is ignored

Callback

If you need a callback for the ad request do these two simple additions.

In HelloWorldScene.h add:

private:
    void onAdsResult(int code, std::string& msg);

And in HelloWorldScene.cpp add:

bool HelloWorld::init()
{
...
        std::function<void(int, std::string&)> callback = std::bind(&HelloWorld::onAdsResult, this, std::placeholders::_1, std::placeholders::_2);

        _adcashSDK->setCallback(a);
    }
    void HelloWorld::onAdsResult(int code, std::string& msg)
    {
        log("OnAdsResult, code : %d, msg : %s", code, msg.c_str());
    }

Sample project

You can find an example implementation of the plugin here.

Support

If you need any support or assistance you can contact the Adcash mobile team by sending an email to [email protected].

  • Did you find this article helpful?
  •    
Tagged: