Native Ad V2 - fan-ADN/nendSDK-cocos2d-x GitHub Wiki
Preparation
If you have not yet register app/ad placements nor downloaded SDK, please refer to following link.
SDK Implementation
Add SDK/Module to a project.
The Flow to Implement Native Ad
It is able to acquire native ad information such as text and image from cocos2d-x module ver2.1.0.
Please follow the instruction below.
Loading Native Ad
At first, generate the instance of NendNativeAdClient
class.
Next, acquire the ad object by using loadAd(const std::function<void (nend_module::NendNativeAd*, NendNativeLoadResultCode, std::string)> &callback)
method which is prepared in NendNativeAdClient
.
Header File
#include "NendNativeAdClient.h"
class YourAppScene : public cocos2d::Layer
{
private:
// Remain the instance within a class
nend_module::NendNativeAdClient* _nativeAdClient;
};
Implementation File
using namespace nend_module;
...
auto apiKey = "ApiKey published on the dashboard";
auto spotId = "SpotId published on the dashboard";
// generate the instance of NendNativeAdClient
_nativeAdClient = new NendNativeAdClient(apiKey, spotId);
// Loading ad info
_nativeAdClient->loadAd([=](NendNativeAd* nativeAd, NendNativeLoadResultCode resultCode, std::string errorMessage){
if (nullptr != nativeAd) {
// Success loading
} else {
// fail loading
}
});
Acquire Ad Information from the Ad Object
After you succeed acquiring ad object, you can acquire each ad data from NendNativeAd
instance of the callback.
The example to acquire “Advertising Explicitly”, “Ad title” and “Ad image” below
// Acquire the text of Advertising Explicitly
prText->setString(nativeAd->prTextForAdvertisingExplicitly(NAD_NATIVE_ADVERTISING_EXPLIICITY_PR));
// Acquire ad title
shortText->setString(nativeAd->getShortText());
// Download the texture of ad image
nativeAd->downloadAdImage([=](Texture2D* adTexture, std::string errorMessage){
if (nullptr != adTexture) {
adImage->setTexture(adTexture);
} else {
// fail loading
}
});
For more information please refer to “The Contents of Native Ad Class”
Activate Ads
Activate ads with activateAdView(Node* node, Label* prLabel)
method of NendNativeAd
, and count impressions.
// Counting impressions and registering for click process.
nativeAd->activateAdView(adLayer, prText);
- Set the
Node
which you placed on ad to the parameterNode* node
No.1. ThisNode
will be the area that can be clicked. - Set the
Label
which you set the Advertising Explicitly to the parameterLabel* prLabel
No.2. It moves to opt out page when the click occurs.
The event will not occur even though you click Node unless you activate the Ad.
Ad impressions will not be counted unless the ad Node show up on the screen.
The Example of the Whole Picture from Generating Client to Displaying Ad
Header File
#include "NendNativeAdClient.h"
class YourAppScene : public cocos2d::Layer
{
private:
// Remain the instance within a class
nend_module::NendNativeAdClient* _nativeAdClient;
};
Implementation File
using namespace nend_module;
...
auto apiKey = "ApiKey published on the dashboard";
auto spotId = "SpotId published on the dashboard";
// Generate NendNativeAdClient instance
_nativeAdClient = new NendNativeAdClient(apiKey, spotId);
LayerColor* adLayer; // Ad View
Label* prText; // Advertising Explicitly
Label* shortText; // Ad title
Sprite* adImage; // Ad image
...
// Loading ad information
_nativeAdClient->loadAd([=](NendNativeAd* nativeAd, NendNativeLoadResultCode resultCode, std::string errorMessage){
if (nullptr != nativeAd) {
// Acquire the text of Advertising Explicitly
prText->setString(nativeAd->prTextForAdvertisingExplicitly(NAD_NATIVE_ADVERTISING_EXPLIICITY_PR));
// Acquire ad title
shortText->setString(nativeAd->getShortText());
// Download the texture of ad image
nativeAd->downloadAdImage([=](Texture2D* adTexture, std::string errorMessage){
if (nullptr != adTexture) {
adImage->setTexture(adTexture);
} else {
// Fail downloading
}
});
// Counting impressions and registering for click process.
nativeAd->activateAdView(adLayer, prText);
} else {
// Fail loading
}
});
Release the Instance
Release the instance when you do not need it any longer.
YourAppScene:~YourAppScene()
{
// Release the instance when you do not need it any longer
delete _nativeAdClient;
}
Auto Ad Reload
Please refer to the link below.
- [Auto Ad Reload](Native Ad#auto_reload)
Register Ad Click Callback
Please refer to the link below.
- [Register Ad Click Callback](Native Ad#click_callback)