Skip to content

Instantly share code, notes, and snippets.

@tklee1975
Last active August 29, 2015 18:31
Show Gist options
  • Save tklee1975/79bee78ad749966ad289 to your computer and use it in GitHub Desktop.
Save tklee1975/79bee78ad749966ad289 to your computer and use it in GitHub Desktop.
AdMob Helper Classes for iOS
//
// AdHelper.cpp
// TapToJump
//
// Created by Ken Lee on 25/8/15.
//
//
#include "AdHelper.h"
// Selective for different platform
#if(CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#include "AdHelperiOS.h"
void AdHelper::showAdmobBanner()
{
AdHelperiOS::showAdmobBanner();
}
void AdHelper::hideAdmobBanner()
{
AdHelperiOS::hideAdmobBanner();
}
#elif CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
#include "AndroidHelper.h"
void AdHelper::showAdmobBanner()
{
AndroidHelper::callVoidStaticMethod("org.cocos2dx.cpp.AppActivity", "showAd");
}
void AdHelper::hideAdmobBanner()
{
AndroidHelper::callVoidStaticMethod("org.cocos2dx.cpp.AppActivity", "hideAd");
}
#else
void AdHelper::showAdmobBanner()
{
log("showAdmobBanner: Feature not support!");
}
void AdHelper::hideAdmobBanner()
{
log("hideAdmobBanner: Feature not support!");
}
#endif
//
// AdHelper.h
// TapToJump
//
// Created by Ken Lee on 25/8/15.
//
//
#ifndef __TapToJump__AdHelper__
#define __TapToJump__AdHelper__
#include <stdio.h>
#include "cocos2d.h"
USING_NS_CC;
class AdHelper {
public:
static void showAdmobBanner();
static void hideAdmobBanner();
};
#endif /* defined(__TapToJump__AdHelper__) */
//
// AdHelperiOS.h
//
// Created by Ken Lee on 25/8/15.
//
//
#ifndef TapToJump_AdHelperiOS_h
#define TapToJump_AdHelperiOS_h
#include "cocos2d.h"
#include <string>
#if(CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
// Implementation at somewhere else
class AdHelperiOS
{
public:
static void showAdmobBanner();
static void hideAdmobBanner();
};
#endif
#endif
//
// AdHelperiOSImpl.m
//
// Created by Ken Lee on 25/8/15.
//
//
#include "AdmobHelper.h"
#pragma mark - C Code
#include "AdHelperiOS.h"
void AdHelperiOS::showAdmobBanner()
{
[[AdmobHelper instance] showBanner];
}
void AdHelperiOS::hideAdmobBanner()
{
[[AdmobHelper instance] hideBanner];
}
//
// AdmobHelper.h
//
// Created by Ken Lee on 25/8/15.
//
//
#import <Foundation/Foundation.h>
#import <GoogleMobileAds/GoogleMobileAds.h>
@interface AdmobHelper : NSObject {
GADBannerView *mBannerView;
}
+ (AdmobHelper *)instance;
- (void)addBanner:(UIView *)parentView size:(GADAdSize)adSize
controller:(UIViewController *)controller;
- (void)hideBanner;
- (void)showBanner;
@end
//
// AdmobHelper.m
//
// Created by Ken Lee on 25/8/15.
//
//
#import "AdmobHelper.h"
static AdmobHelper *helper = nil;
#define kAdUnitID @"<your ad unit id>"
@implementation AdmobHelper
+ (AdmobHelper *)instance
{
if(helper == nil) {
helper = [[AdmobHelper alloc] init];
}
return helper;
}
- (void)addBanner:(UIView *)parentView size:(GADAdSize)adSize
controller:(UIViewController *)controller;
{
GADBannerView *view = [[GADBannerView alloc] initWithAdSize:adSize];
// Set the position of the view to bottom
CGFloat y = parentView.frame.size.height - view.frame.size.height;
CGRect newFrame = view.frame; newFrame.origin.y = y;
view.frame = newFrame;
//
[parentView addSubview:view];
// Set the reference
mBannerView = view;
// Replace this ad unit ID with your own ad unit ID.
view.adUnitID = kAdUnitID;
view.rootViewController = controller;
GADRequest *request = [GADRequest request];
// Requests test ads on devices you specify. Your test device ID is printed to the console when
// an ad request is made. GADBannerView automatically returns test ads when running on a
// simulator.
request.testDevices = @[];
[view loadRequest:request];
[self hideBanner];
}
- (void)hideBanner
{
[mBannerView setHidden:YES];
}
- (void)showBanner
{
[mBannerView setHidden:NO];
}
@end
@tklee1975
Copy link
Author

These codes are used by cocos2d-x project;

Find AppController.mm and a method called "- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions ...";
And then place
[[AdmobHelper instance] addBanner:eaglView size:kGADAdSizeSmartBannerPortrait controller:_viewController];
below statement " _viewController.view = eaglView;""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment