我有一个使用AdMob Mediation服务的测试应用程序设置,目前只在测试设备上使用。我根据文档设置了所有必需的方法。我遇到了一个问题,即如果发生“无法接收广告”错误,则不会再请求或显示广告?
部首:
#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文件
#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
我在日志中看到的是'Ad Received'几次,然后'Error - 确实无法接收广告'...在此日志之后没有其他条目就像它停止请求一样?目前仅在模拟器上进行测试。
任何想法如何解决这个问题,或者在收到错误/没有广告时隐藏视图的替代方法?
答案 0 :(得分:1)
我发现同样的事情 - 隐藏GADBannerView
时,不再发出请求。
我成功尝试的一件事是将GADBannerView
移到屏幕外而不是隐藏它。当然,您只想在didFailToReceiveAdWithError
后执行此操作,然后在adViewDidReceiveAd
时将其移回屏幕。我得到了这个工作,所以当广告出现时,用户会看到一个漂亮的动画,就像iAd鼓励一样。
简而言之,下面的代码会将您的GADBannerView
(此处称为mAdBannerView
)放在屏幕底部或屏幕外,具体取决于布尔值adIsLoaded
。
CGRect bannerFrame = mAdBannerView.frame;
bannerFrame.origin.y = self.view.bounds.size.height - (adIsLoaded * bannerFrame.size.height);
mAdBannerView.frame = bannerFrame;
答案 1 :(得分:0)
bannerView_.hidden = 1;
如果出现错误,将隐藏视图,如果收到的广告没有错误,则可能会自动显示
答案 2 :(得分:0)
认为你最好只隐藏带有隐藏属性的bannerView_。
- (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error {
bannerView_.hidden = YES;
}
当然,您必须记得在广告成功收到后将hidden
设置回YES
。
答案 3 :(得分:0)
简单的解决方案,在adView:didFailToReciewvwAdWithError
方法中设置bannerView_.hidden为true。并使用adViewDidReceiveAd
方法检索视图。示例代码:
这些是ADmob的委托方法:
- (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error
{
bannerView_.hidden = YES;
}
- (void)adViewDidReceiveAd:(GADBannerView *)view
{
bannerView_.hidden = NO;
}
答案 4 :(得分:0)
我有同样的问题,这对我有用:
请勿使用.hidden属性隐藏AdMob广告。 只需将alpha设置为0(不可见)或1(可见)。
所以在你的GADBannerView委托方法......
-(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;
}];
}
关于“在此日志之后没有其他条目,它就像它停止请求?”
当我从视图层次结构中删除广告时,这种情况也会发生。但是,当我将广告添加回视图层次结构时,请求会继续。他们没有继续的唯一一次是我使用.hidden属性。