The recently released Version 2.0 of cocos2d for iPhone deprecated the use of RootViewController in cocos2d projects. Previously, we showed you how to integrate AdMob ads by leveraging the RootViewController. Here, we show you the updated way of integrating AdMob ads into cocos2d v2.0 projects.

All of your initialization can now be done in the layer where you’d like the ad to show. In your layer’s init function, you can set up your ad banner as you would normally.

-(id) init
    // Do other layer initialization here.

    adBanner_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
    adBanner_.adUnitID = @"YOUR_ADMOB_PUBLISHER_ID";
    adBanner_.delegate = self;

The tricky part here is setting the rootViewController property for your GADBannerView and putting it into your view hierarchy. Version 2.0 of cocos2d uses a UINavigationController as its top-level view controller. The main OpenGL view is placed inside this controller. You want to set your rootViewController to be the main navigation controller.

AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
[adBanner_ setRootViewController:[app navController]];

There are performance considerations to think about when adding a GADBannerView into your hierarchy. Overlaying UIKit layers on top of OpenGL layers can lead to lower frame rates for apps that draw often. However, with newer hardware, this performance decrease is less of a concern. As always, we recommend that you profile your application’s performance to determine what works in your situation.

For our example, we will add the GADBannerView on top of the OpenGL view. This involves accessing the view being shown from the sharedDirector object.

[[CCDirector sharedDirector].view addSubview:adBanner_];

Remember that since you are creating GADBannerView objects, you will have to clean them up with dealloc as well.

- (void) dealloc {
  adBanner_.delegate = nil;
  [adBanner_ release];
  [super dealloc];

You should now see AdMob ads show up in your cocos2d application. If there are any other integration topics you would like to see, or any technical questions you have, please let us know about them on the forum or check out our G+ page.