Hiding Ad Banner causes no further Ads to be reque

2019-07-04 22:55发布

问题:

I have a Test application setup with AdMob Mediation service being used, only on testing device at the moment. I have setup all the required methods per the documentation. I am having an issue where when the Fail to Receive Ad error occurs, no more ads are requested or shown?

Header:

#import <UIKit/UIKit.h>
#import "GADBannerViewDelegate.h"

@class GADBannerView, GADRequest;

@interface AdTestViewController : UIViewController
    <GADBannerViewDelegate> {
    GADBannerView *bannerView_;
}

@property (nonatomic, retain) GADBannerView *bannerView;

- (GADRequest *)createRequest;

@end

Imp File

#import "AdTestViewController.h"
#import "Constants.h"
#import "GADBannerView.h"
#import "GADRequest.h"

@implementation AdTestViewController

@synthesize bannerView = bannerView_;

- (void)viewDidLoad {
    [super viewDidLoad];

    // Create a view of the standard size at the top of the screen.
    // Available AdSize constants are explained in GADAdSize.h.
    //bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
    // Initialize the banner at the bottom of the screen.
    //CGPoint origin = CGPointMake(0.0,
     //                            self.view.frame.size.height -
       //                          CGSizeFromGADAdSize(kGADAdSizeBanner).height);
    self.bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
                                                    //origin:origin];

    self.bannerView.adUnitID = kAdMobPublisherID;
    self.bannerView.delegate = self;
    [self.bannerView setRootViewController:self];
    [self.view addSubview:self.bannerView];
    self.bannerView.center =
    CGPointMake(self.view.center.x, self.bannerView.center.y);
    [bannerView_ loadRequest:[self createRequest]];

    bannerView_.backgroundColor = [UIColor blueColor];


    // Make the request for a test ad. Put in an identifier for
    // the simulator as well as any devices you want to receive test ads.
    GADRequest *request = [GADRequest request];
    request.testDevices = [NSArray arrayWithObjects:
                           @"4D047EB9-A3A7-441E-989E-C5437F05DB04",
                           @"YOUR_DEVICE_IDENTIFIER",
                           nil];

}

- (GADRequest *)createRequest {
    GADRequest *request = [GADRequest request];

    // Make the request for a test ad. Put in an identifier for the simulator as
    // well as any devices you want to receive test ads.
    request.testDevices = [NSArray arrayWithObjects:
                           @"4D047EB9-A3A7-441E-989E-C5437F05DB04",
                           @"YOUR_DEVICE_IDENTIFIER",
                           nil];
    return request;
}

- (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error;
{
    NSLog(@"Error - did Fail to Receive an Ad");
    bannerView_.hidden = YES;

}

- (void)adViewDidReceiveAd:(GADBannerView *)view;
{
    NSLog(@"Ad Received");
    bannerView_.hidden = NO;
}

@end

What I am seeing in my logs is the 'Ad Received' a few times, then 'Error - did Fail to Receive an Ad'... After this log there are no further entries it is like it stops requesting? Testing only on simulator at present.

Any ideas how to solve this, or potentially an alternative method on hiding the view when an error/no ad is received?

回答1:

I find the same thing – when the GADBannerView is hidden, no more requests are sent out.

One thing I tried successfully is to move the GADBannerView offscreen instead of hiding it. Of course, you only want to do this as a consequence of didFailToReceiveAdWithError, and then move it back onscreen when adViewDidReceiveAd. I got this working so the user sees a nice animation when ads come and go, much like iAd encourages.

In short, the code below will place your GADBannerView (here called mAdBannerView) either at the bottom of the screen or offscreen, depending on the boolean adIsLoaded.

  CGRect bannerFrame = mAdBannerView.frame;
  bannerFrame.origin.y = self.view.bounds.size.height - (adIsLoaded * bannerFrame.size.height);
  mAdBannerView.frame = bannerFrame;


回答2:

in the method that's called when there is an error put in some thing like

bannerView_.hidden = 1;

that will hide the view if there's an error and it will probably automatically be displayed if an ad was received with no error



回答3:

Think you're better off just hiding the bannerView_ with the hidden property.

- (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error {
        bannerView_.hidden = YES;       
    }

Of course you have to remember to set hidden back to YES when an ad is successfully received.



回答4:

Simple solution, set the bannerView_.hidden true in adView:didFailToReciewvwAdWithError method. And to retrieve the view use adViewDidReceiveAd method. Example code:

These are ADmob's delegate method:

- (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error
{
    bannerView_.hidden = YES;
}

- (void)adViewDidReceiveAd:(GADBannerView *)view
{
    bannerView_.hidden = NO;
}


回答5:

I had the same problem, this worked for me:

Do not use the .hidden property to hide AdMob ads. Just set the alpha to 0 (invisible) or 1 (visible).

So in your GADBannerView delegate method...

-(void)adView:(GADBannerView *)bannerView didFailToReceiveAdWithError:(GADRequestError *)error {

    // Hide the ad banner.
    [UIView animateWithDuration:0.5 animations:^{

        self.myADBanner.alpha = 0.0;

    }];

}

-(void)adViewDidReceiveAd:(GADBannerView *)bannerView {

    //Show the ad banner.
    [UIView animateWithDuration:0.5 animations:^{

        self.myADBanner.alpha = 1.0;

    }];

}

With regards to "After this log there are no further entries it is like it stops requesting?"

This happens to me as well when I remove an ad from the view hierarchy. However, requests continue when I add the ad back to the view hierarchy. The only time they didn't continue was when I was using the .hidden property.



标签: ios ios5 admob iad