Monday, October 3, 2011

AdMob/Cocos2D: How to implement Admod using Cocos2D

How's it going, bros?


Here's another post for those who want to try to make some money with Ads, specifically with AdMob - Google solutions for apps in your mobile application.


If you're just looking for the standard stuff for a UIViewController you can go directly to the GoogleCode page:


http://code.google.com/mobile/ads/docs/ios/fundamentals.html

Real easy instructions, almost thought that they should put on the title: "AdMob implementation for Dummies" (that's why i used :) ).


But if you are a avid Cocos2D programmer and are cracking you head of testing your options and not getting a good tutorial here it goes:


*Using: Cocos2D 1.0.1


1)Follow Google's link instructions till the part it shows the ".h" file;
2) Include what's not there:


//MyCoolApp.h


#import "GADBannerView.h"
#import "RootViewController.h"


@interface MyCoolApp : CCLayer {
    
    GADBannerView *bannerView_; 

    
    NSTimer* refreshTimer;
    NSInteger elapsedTime;
    NSInteger minutes;
    NSInteger seconds;



}



@property(nonatomic, retain)NSTimer* refreshTimer;

//YES I DID START THIS METHOD WITH UPPER CASE (:
-(void)AdMob;
-(void)removeSubviews;

-(void)targetMethod:(NSTimer *)theTimer;




//MyCoolApp.m

@synthesize refreshTimer;





//AdMob code

-(void)AdMob{
    NSLog(@"ADMOB");
    
    CGSize size = [[CCDirector sharedDirector] winSize];
    bannerView_ = [[GADBannerView alloc]
                   initWithFrame:CGRectMake(0.0, size.height-GAD_SIZE_468x60.height-720,GAD_SIZE_468x60.width,GAD_SIZE_468x60.height)];
    
    bannerView_.adUnitID = @"BUNCH_OF_NUMBERS_ADMOB_GIVES_YOU";
    bannerView_.rootViewController = self;
    [[[CCDirector sharedDirector]openGLView]addSubview:bannerView_];
    [bannerView_ loadRequest:[GADRequest request]];
}



//best practice for removing the barnnerView_

-(void)removeSubviews{
    NSArray* subviews = [[CCDirector sharedDirector]openGLView].subviews;
    for (id SUB in subviews){
        [(UIView*)SUB removeFromSuperview];
        [SUB release];
    }
}

//this makes the refreshTimer count
-(void)targetMethod:(NSTimer *)theTimer{
    
    //INCREASE OF THE TIMER AND SECONDS
    elapsedTime++; 
    seconds++;
    
    //INCREASE OF THE MINUTOS EACH 60 SECONDS
    if (seconds>=60) {
        seconds=0; minutes++;
        [self removeSubviews];
        [self AdMob];
    }
    NSLog(@"TIME: %02d:%02d", minutes, seconds);
}






-(id)init{
    
    if( (self=[super init])) {
             refreshTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(targetMethod:) userInfo:nil repeats:YES];
            [self AdMob];
    }
    return self;
}



3)Just be happy!




Hope this helps you guys, if you have any problems just get in contact.



8 comments:

  1. Hello Filipe, I understand that this code is for iphone platform. Can u help me out with the same for android.

    Thanks in advance.

    ReplyDelete
    Replies
    1. relax, no problem with the spelling.
      i really don't do android, put you can show me the code and i can try to help with any concept i can.

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi,

    I have used your admob coding in my game, and its working but i have two issue:
    1.subview is not removing from superview.

    2.when I tap on admob banner then some ads load and most of ads not loading.

    can you please tell me what i am doing mistake and how can i solve these two issue.

    Thanks!

    ReplyDelete
    Replies
    1. Could you post some code or send me what ur using to implement admob?

      Delete
    2. I have used full your code only, i have used your code in some classes in which i want to show admob banner.

      Delete
  4. Hi Felipe here is my code

    ` -(void)AdMob{
    NSLog(@"ADMOB");


    CGSize winSize = [[CCDirector sharedDirector]winSize];
    // Create a view of the standard size at the bottom of the screen.


    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){

    bannerView_ = [[GADBannerView alloc]
    initWithFrame:CGRectMake(size.width/2-364,
    size.height -
    GAD_SIZE_728x90.height,
    GAD_SIZE_728x90.width,
    GAD_SIZE_728x90.height)];

    }
    else { // It's an iPhone

    bannerView_ = [[GADBannerView alloc]
    initWithFrame:CGRectMake(size.width/2-160,
    size.height -
    GAD_SIZE_320x50.height,
    GAD_SIZE_320x50.width,
    GAD_SIZE_320x50.height)];
    }


    // CGSize size = [[CCDirector sharedDirector] winSize];
    // bannerView_ = [[GADBannerView alloc]
    // initWithFrame:CGRectMake(0.0, size.height-GAD_SIZE_468x60.height-720,GAD_SIZE_468x60.width,GAD_SIZE_468x60.height)];



    // bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
    bannerView_.adUnitID =@"a15062384653c9e";
    }
    else {
    bannerView_.adUnitID =@"a15062392a0aa0a";
    }

    bannerView_.rootViewController = self;
    [[[CCDirector sharedDirector]openGLView]addSubview:bannerView_];
    [bannerView_ loadRequest:[GADRequest request]];


    GADRequest *request = [[GADRequest alloc] init];
    request.testing = [NSArray arrayWithObjects:
    GAD_SIMULATOR_ID, nil]; // Simulator

    [bannerView_ loadRequest:request];
    }



    //best practice for removing the barnnerView_

    -(void)removeSubviews{
    NSArray* subviews = [[CCDirector sharedDirector]openGLView].subviews;
    for (id SUB in subviews){
    [(UIView*)SUB removeFromSuperview];
    // [SUB release];
    }
    NSLog(@"remove from view");
    }

    //this makes the refreshTimer count
    -(void)targetMethod:(NSTimer *)theTimer{

    //INCREASE OF THE TIMER AND SECONDS
    elapsedTime++;
    seconds++;

    //INCREASE OF THE MINUTOS EACH 60 SECONDS
    if (seconds>=60) {
    seconds=0; minutes++;
    [self removeSubviews];
    [self AdMob];
    }
    // NSLog(@"TIME: %02d:%02d", minutes, seconds);
    }


    `

    my issue is admob banner not getting remove from layer its also available in which class i have not use admob.

    ReplyDelete

Tweet