我在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标题的引用:
/// 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;
这表明即使视图不在层次结构中也应该可以接收那些委托回调,这正是我想要的。那么,任何想法我怎么能实现这个目标?
答案 0 :(得分:6)
好的,这里的问题是我没有保留对bannerView
的引用。它在返回方法后被释放,这就是没有调用委托方法的原因。
答案 1 :(得分:4)
从 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
答案 2 :(得分:0)
您应该将GADBannerView
添加到视图中,并将其隐藏属性设置为YES
。另外,我建议使用AdMob提供的AdSize常量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
来为此设置动画。属性。然后,在收到广告时为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
是透明的。因此,将其添加到您的视图中并且不执行任何其他操作也会起作用。