Admob banner not getting remove from superview
- by Anil gupta
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);
}
`