简体   繁体   中英

AdMob Banner Not Showing

I am trying to programatically create a banner ad using the following code:

+ (void)createBanner:(UIViewController *)sender
{
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
    {
        bannerHeight = 50;
    }
    else
    {
        bannerHeight = 90;
    }

    GADRequest *request = [GADRequest request];
    // request.testDevices = @[ @"testDeviceNumberIDNumber", @"Simulator"];
    // request.testDevices = [NSArray arrayWithObjects:GAD_SIMULATOR_ID, nil];

    bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
    bannerView.adUnitID = @"MY ID"; // I have replaced my AdMob ID for posting this question with "MY ID"; usually the "ca-app-pub-xxxxx.." ID is in this line
    bannerView.rootViewController = (id)self;
    bannerView.delegate = (id<GADBannerViewDelegate>)self;

    senderView = sender.view;

    bannerRect = [bannerView bounds];

    bannerView.frame = CGRectMake(0, 100, senderView.frame.size.width, bannerHeight);

    [bannerView loadRequest:request];

    containerView = [[UIView alloc] initWithFrame:senderView.frame];

    bannerContainerView = [[UIView alloc] initWithFrame:CGRectMake(0, senderView.frame.size.height, senderView.frame.size.width, bannerHeight)];

    for (id object in sender.view.subviews) { 
        [object removeFromSuperview];
        [containerView addSubview:object];
    }

    [senderView addSubview:containerView];
    [senderView addSubview:bannerContainerView];
}

+ (void)adViewDidReceiveAd:(GADBannerView *)view
{
    [UIView animateWithDuration:0.5 animations:^{
        containerView.frame = CGRectMake(0, 0, senderView.frame.size.width, senderView.frame.size.height - bannerHeight);
        bannerContainerView.frame = CGRectMake(0, senderView.frame.size.height - bannerHeight, senderView.frame.size.width, bannerHeight);
        [bannerContainerView addSubview:bannerView];
    }];
}

This code works in one of my old apps when I run it, but when I run it in my new app, it refuses to load, whether I run it with test ads or not.

Now for a potentially significant detail: In the .h file of the ViewController , the interface line reads @interface ViewController : UIViewController<UIAlertViewDelegate> , but the view controller is embedded in a Navigation Controller , as shown when I load the app, which has a navigation bar at the top.

Would this be the reason the banner is not loading, and if so, how may I fix this?

All help appreciated.

I recall dealing with this issue by caching the bannerView object in a collection.

Somewhere in code:

@property (strong, nonatomic) NSMutableArray * googleAdsCollection;

// in viewDidLoad 
- (void)viewDidLoad {
    self.googleAdsCollection = [NSMutableArray array];
    // ... your code here ...
}
- (void)createBanner {
   // in your create banner perhaps at the end
   [self.googleAdsCollection addObject:bannerView];
   // ... your cod here ...
}

// inside the adView delegates - received, failed, what else
[self.googleAdsCollection removeObject:view]

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