简体   繁体   中英

Can't remove Admob Banner from the view (iOS)

I have implemented Admob into my app but i've noticed that if the Admob view doesn't receive an ad, I can't remove it from the superview. If it already has an ad loaded it just stays there with that ad loaded even if the device is not connected to the internet. This is my code:

self.admobBannerView = [[GADBannerView alloc] init];
self.admobBannerView.frame = CGRectMake(0.0,self.view.frame.size.height-50,
                                                  GAD_SIZE_320x50.width,
                                                  GAD_SIZE_320x50.height);

self.admobBannerView.adUnitID = @"...";
self.admobBannerView.rootViewController = self;
self.admobBannerView.delegate = self;
[self.view addSubview:self.admobBannerView];
[self.admobBannerView loadRequest:[GADRequest request]];

Then the Admob delegate

- (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error {
   [self.admobBannerView removeFromSuperview];
}

Despite calling [self.admobBannerView removeFromSuperview]; the banner remains where it is. I can't understand why this is happening. Any help is appreciated. Thanks

I had the same problem and debugged the view hierarchy w/ Xcode only to find multiple GADBannerViews existed. Fixed the code to check if ad view already existed before adding one.

In your case you should wrap the ad view creation in:

if (self.admobBannerView != nil)
{
  // create ad
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM