Gamer
Gamer

Reputation: 513

Admob banner not getting remove from superview

I am developing one 2d game using cocos2d framework, in this game i am using admob for advertising, in some classes not in all classes but admob banner is visible in every class and after some time game getting crash also.

I am not getting how admob banner is comes in every class in fact i have not declare in Rootviewcontroller class. can any one suggest me how to integrate Admob in cocos2d game, i want Admob banner in particular classes not in every class, I am using latest google admob sdk, my code is below:

Thanks in advance

`

-(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)];
}




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);
 }

`

Upvotes: 1

Views: 2430

Answers (3)

RelativeGames
RelativeGames

Reputation: 1593

Full fix (tested with iOS 8.1 and Admob 6.12.0)

-(void)RemoveAds
{
    if (adBanner != nil)
    {
        [adBanner setRootViewController:nil];
        [adBanner removeFromSuperview];
        adBanner = nil;
    }
}

Upvotes: 1

Guru
Guru

Reputation: 22042

UPDATES: Refer generalised answer here: Admob-banner-integration-in-cocos2d

I hope already u got solution. If not then here is all code for Admob integration in cocos2D game.

 #define ENABLE_ADMOB 1
 //#define COCOS2D_2_0 1

@interface MyMainMenu : CCLayer
{
    #ifdef ENABLE_ADMOB
    GADBannerView *mBannerView;
    #endif
}

@implementation MyMainMenu

-(void)onEnter
{
    [super onEnter];

    #ifdef ENABLE_ADMOB
       #ifdef COCOS2D_2_0
         AppController *app =  (AppController*)[[UIApplication sharedApplication] delegate];    
       #else 
         AppDelegate* app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
       #endif
    mBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];

    // Specify the ad's "unit identifier." This is your AdMob Publisher ID.
    mBannerView.adUnitID = MY_BANNER_UNIT_ID;

    // Let the runtime know which UIViewController to restore after taking
    // the user wherever the ad goes and add it to the view hierarchy.


    //size


    #ifdef COCOS2D_2_0
    mBannerView.rootViewController = app.navController;
    [app.navController.view addSubview:mBannerView];
    #else 
    mBannerView.rootViewController = app.viewController;
    [app.viewController.view addSubview:mBannerView];
    #endif

    // Initiate a generic request to load it with an ad.
    [mBannerView loadRequest:[GADRequest request]];

    CGSize AdSize = kGADAdSizeBanner.size;

    CGRect frame = mBannerView.frame;
    frame.origin.y = -50.0f;

    #ifdef COCOS2D_2_0
    frame.origin.x = (app.navController.view.bounds.size.width - AdSize.width) / 2 ;
    #else
    frame.origin.x = (app.viewController.view.bounds.size.width - AdSize.width) / 2 ;
    #endif

    mBannerView.frame = frame;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

    frame = mBannerView.frame;
    frame.origin.y = 0.0f;

    mBannerView.frame = frame;
    [UIView commitAnimations];    

    #endif
}

-(void)showBannerView
{
    if (mBannerView) 
    {
        [UIView animateWithDuration:0.5
                              delay:0.1
                            options: UIViewAnimationCurveEaseOut
                         animations:^
         {
             CGRect frame = mBannerView.frame;
             frame.origin.y = 0.0f;
             mBannerView.frame = frame;
         } 
                         completion:^(BOOL finished)
         {
         }];
    }

}


-(void)hideBannerView
{
    if (mBannerView) 
    {
        [UIView animateWithDuration:0.5
                              delay:0.1
                            options: UIViewAnimationCurveEaseOut
                         animations:^
         {
             CGRect frame = mBannerView.frame;
             frame.origin.y = -50.0f;
             mBannerView.frame = frame;
         } 
                         completion:^(BOOL finished)
         {
         }];
    }

}


-(void)dismissAdView
{
#ifdef ENABLE_ADMOB
    if (mBannerView) 
    {
        [UIView animateWithDuration:0.5
                              delay:0.1
                            options: UIViewAnimationCurveEaseOut
                         animations:^
         {
             CGRect frame = mBannerView.frame;
             frame.origin.y = -50.0f;
             mBannerView.frame = frame;
         } 
                         completion:^(BOOL finished)
         {
             [mBannerView setDelegate:nil];
             [mBannerView removeFromSuperview];
             mBannerView = nil;

         }];
    }
#endif  

}

Upvotes: 3

Abdullah Umer
Abdullah Umer

Reputation: 4624

Since in cocos2d, you will have different classes for scenes.

My suggestion would be to create a separate class for the add banner and have a static method do the job for you. You will have to save a reference of the add banner in that class and by using the static methods you can add/remove it to/from the openglview.

For removing you will only do: [bannerView removeFromSuperview];

Upvotes: 0

Related Questions