HOWTO: Integrate Admob / iAds into iOS with cocos2d-x

Hello I just finished doing this so I thought I’d share the code if anyone else was stuck. This is a very simple implementation and I’ll update the code if I make it more robust. This was borrowed from a great tutorial from plaincode’s blog.

I extended it out just slightly to use Admob as a fallback. Obviously it’ll need tweaking for different banner sizes. I plan to update it later to support interstitial ads as well.

You can get the code here:
ssh://gitbitbucket.org/6foot3foot/cocos2d-x.git@

This uses Apple provided sample code.  It has been
modified to use AdMob ads as a fall back.

The initial instructions came from this blog:
http://plaincode.blogspot.cm/2013/01/how-to-integrate-iad-to-your-cocos2d-x.html

Use that to setup the iAd portion.

Afterwards setup Google Admobs, by downloading the SDK and including it in your
project.  Add the library to your build phases, and set the -ObjC flag to 
"Other Linker Flags" in your build settings.

Add the following frameworks for Admob to your Link Binary with Library phase:
MessageUI.framework
SystemConfiguration.framework
StoreKit.framework
AdSupport.framework
libGoogleAdMobAds.a

The rest should just work.

Hopefully this is helpful to others.

Hello Michael,
I have been trying to integrate Admob to our iOS app. There are so many sample about “How to integrate Admob to iOS app”.
I am totally new to Objective C, so having too much difficulty to understand whats going on.

What I need is to show Admob ads on a particular layer (CCLayer) in our app. All the samples on the web are about adding google ad banner to the root view (AppController.mm). By that technique I cannot control when to show/hide that ad banner.

Is it possible to create ad banner object in my one scene/layer (C++) and calling show/hide on that object.
If yes, can you help me to achieve that? Any help appreciated.

Hi Manish,

Even if you have it in a layer your ads will be drawn within their own UIView, so moving the layer would have little to no effect, the ads are part of the iOS view hierarchy, the whole cocos2dx view is just a single view, regardless of your layers, sprites, etc. So you’ll need to stack a new view on top which is your ads, and your game will be under it.

I hope that makes sense… But for the most part, pull down the code from the bitbucket I posted, and then try the methods for the hide / show, and see if that’s what you want. You can even extend it to not use iAds if you wish to not have them, (although it will fall back automatically anyway, but if you didn’t want to include the sdk).

To enable / hide the ads i’m just using NSNotifications, so I scribbled up a small ‘bridge’ class for that, I’ve added that to the repo as well.

SFBridgeController::enableAds()

Hope that helps

Hi Michael,

Thanks for the response. I got all your points. “SFBridgeController” seems to be right choice.
Just one more silly question, I’m having a difficulty find where to hook this BannerViewController. I have an impression to put that somewhere in AppController.mm.

What I tried in AppController.mm is (not sure if this is right):

// AppController.mm
[[BannerViewController alloc] initWithContentViewController: DONT_KNOW_WHAT_TO_PASS_HERE];

I tried different values for DONT_KNOW_WHAT_TO_PASS_HERE like “nil” (Assertion fail, I knew it would happen), and other things too but not in the right direction.

Thanks in advance.