3

我正在使用 iOS 上的 Google Mobile Ads 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不在视图层次结构中,则根本不会调用委托方法。此外,我找到了这个答案,这与我所观察到的一致。另一方面,这是来自 GADBannerViewDelegate 标头的引用:

/// 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;

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

4

3 回答 3

7

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

于 2016-07-02T15:14:03.353 回答
5

从 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

于 2021-03-18T09:28:09.857 回答
0

您应该将其添加GADBannerView到您的视图中并将其隐藏属性设置为YES最初。另外,我建议使用kGADAdSizeBannerAdMob 提供的 AdSize 常量。以下是其他AdSize 常量的列表。

例如:

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属性来制作动画。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当没有广告可显示时,它是透明的。因此,将其添加到您的视图中并且什么也不做也可以。

于 2016-06-10T19:45:42.170 回答