This guide demonstrates how to integrate Interstitial Ad Unit in a new cocos2d-x project.
1. Install the SDK
Follow the instructions and install the SDK manually.
2. Initialize the SDK
- Rename HelloWorldScene.cpp to HelloWorldScene.mm.
- Add the following code to bool HelloWorld::init() in HelloWorldScene.mm:
[[DIOController sharedInstance] initializeWithAppId:@"7522" completionHandler:^{
NSLog(@"CONTROLLER INITIALIZED");
} errorHandler:^(NSString *error) {
NSLog(@"%@", error);
}];
3. Show Ad
a. In case of AdMob Mediation Adapter integration:
- Add these Objective-C classes into your project:
- DIOAdmobInterstitialAdapter.h
- DIOAdmobInterstitialAdapter.m
For each of the added .m files (under Target / Build Phases / Compile Sources):
add -fobj-arc in compiler flags.
- Follow AdMob Mediation Adapter integration guide.
b. In case of MoPub Mediation Adapter integration:
- Add these Objective-C classes into your project:
- DIOMopubInterstitialAdapter.h
- DIOMopubInterstitialAdapter.m
For each of the added .m files (under Target / Build Phases / Compile Sources):
add -fobj-arc in compiler flags.
- Follow MoPub Mediation Adapter integration guide.
c. In case of direct SDK integration follow the steps below:
- In void HelloWorld::menuCloseCallback(Ref*) in HelloWorldScene.mm, comment the line which closes the game.
- Then add the following:
if ([DIOController sharedInstance].initialized) {
DIOPlacement *placement = [[DIOController sharedInstance] placementWithId:@"<PLACEMENT-ID>"];
DIOAdRequest *adRequest = [placement newAdRequest];
[adRequest requestAdWithAdReceivedHandler:^(DIOAdProvider *adProvider) {
NSLog(@"AD RECEIVED");
[adProvider loadAdWithLoadedHandler:^(DIOAd *ad) {
NSLog(@"AD LOADED");
UIViewController *viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
[ad showAdFromViewController:viewController eventHandler:^(DIOAdEvent event){
switch (event) {
case DIOAdEventOnShown:
NSLog(@"AdEventOnShown");
break;
case DIOAdEventOnClicked:
NSLog(@"AdEventOnClicked");
break;
case DIOAdEventOnFailedToShow:
NSLog(@"AdEventOnFailedToShow");
break;
case DIOAdEventOnClosed:
NSLog(@"AdEventOnClosed");
break;
case DIOAdEventOnAdCompleted:
NSLog(@"AdEventOnAdCompleted");
}
}];
} failedHandler:^(NSString *message){
NSLog(@"AD FAILED TO LOAD: %@", message);
}];
} noAdHandler:^{
NSLog(@"NO AD");
}];
}
Then run and press the close button to show Ad Unit.
Comments
0 comments
Please sign in to leave a comment.