如果视图不在视图层次结构中,则不调用 GADBannerView 委托方法

GADBannerView delegate methods not called if the view is not in the view hierarchy

我正在 iOS 上使用 Google 移动广告 SDK 并尝试展示一些广告。我的代码:

GADBannerView* bannerView = [[GADBannerView alloc] initWithAdSize:GADAdSizeFromCGSize(CGSizeMake(300, 250))];
bannerView.adUnitID = @"hidden";
bannerView.rootViewController = self;
bannerView.delegate = self;

GADRequest* request = [GADRequest request];
request.testDevices = @[ kGADSimulatorID ];

[bannerView loadRequest:request];

如果我将 bannerView 添加到您在上面看到的代码之后的视图层次结构中,这会很好地工作。但是,我真的不想在广告加载之前添加它,所以我想延迟它。我注意到如果 bannerView 不在视图层次结构中,则根本不会调用委托方法。此外,我发现 this answer,这与我观察到的一致。另一方面,这是来自 GADBannerViewDelegate header:

的引述
/// Tells the delegate that an ad request successfully received an ad. The delegate may want to add
/// the banner view to the view hierarchy if it hasn't been added yet.
- (void)adViewDidReceiveAd:(GADBannerView *)bannerView;

这表明即使视图不在层次结构中,也应该可以接收这些委托回调,这正是我想要的。那么,有什么想法可以实现吗?

您应该将 GADBannerView 添加到您的视图中,并将其隐藏的 属性 初始设置为 YES。另外,我建议使用 AdMob 提供的 AdSize Constant kGADAdSizeBanner。这是其他 AdSize Constants.

的列表

例如:

bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
bannerView.adUnitID = @"YourAdUnitID";
bannerView.rootViewController = self;
bannerView.delegate = self;
[bannerView loadRequest:[GADRequest request]];
bannerView.hidden = YES; // Hide banner initially
[self.view addSubview:bannerView];
// This will put the banner at the bottom of the screen and stretch to fit the screens width
[bannerView setFrame:CGRectMake(0, self.view.frame.size.height - bannerView.frame.size.height, self.view.frame.size.width, bannerView.frame.size.height)];

然后,当您收到广告时取消隐藏横幅。例如:

-(void)adViewDidReceiveAd:(GADBannerView *)adView {
    // We've received an ad so lets show the banner
    bannerView.hidden = NO;
    NSLog(@"adViewDidReceiveAd");
}

-(void)adView:(GADBannerView *)adView didFailToReceiveAdWithError:(GADRequestError *)error {
    // Failed to receive an ad from AdMob so lets hide the banner
    bannerView.hidden = YES;
    NSLog(@"adView:didFailToReceiveAdWithError: %@", [error localizedDescription]);
}

如果您愿意,您也可以通过将横幅的 alpha 属性 初始设置为 0.0 而不是使用它的 hidden [=32 来制作动画=].然后,在收到广告时为 alpha 设置动画。例如:

-(void)adViewDidReceiveAd:(GADBannerView *)adView {
    // We've received an ad so lets fade in the banner
    [UIView animateWithDuration:0.2 animations:^{
        bannerView.alpha = 1.0;
    }];
    NSLog(@"adViewDidReceiveAd");
}

-(void)adView:(GADBannerView *)adView didFailToReceiveAdWithError:(GADRequestError *)error {
    // Failed to receive an ad from AdMob so lets fade out the banner
    [UIView animateWithDuration:0.2 animations:^{
        bannerView.alpha = 0.0;
    }];
    NSLog(@"adView:didFailToReceiveAdWithError: %@", [error localizedDescription]);
}

此外,作为旁注,GADBannerView 在没有要显示的广告时是透明的。因此,将它添加到您的视图中并且什么都不做也可以。

好的,所以这里的问题是我没有保留对 bannerView 的引用。它在方法返回后被释放,这就是委托方法没有被调用的原因。

我刚从 Admob SDK 7.56 升级到 8.2 后遇到了同样的问题:

他们更改了 GADBannerViewDelegate 协议的方法名称。
例如。而不是

-(void)adViewDidReceiveAd:(GADBannerView *)adView;

现在

-(void)bannerViewDidReceiveAd:(GADBannerView *)bannerView;

另请参阅 Admob SDK 版本 8 的迁移指南:
https://developers.google.com/admob/ios/migration#methods_removedreplaced