我使用ADBannerView加载广告,并使用bannerLoaded属性来检测结果。 bannerLoaded始终为NO,但ADBannerView实际上成功加载了广告(我在if语句中反向bannerLoaded来证明这一点)。并且bannerView:didFailToReceiveAdWithError:不记录任何错误。
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self load_iAd];
}
- (void)load_iAd
{
//init iAd
// On iOS 6 ADBannerView introduces a new initializer, use it when available.
if ([ADBannerView instancesRespondToSelector:@selector(initWithAdType:)]) {
_bannerView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
} else {
_bannerView = [[ADBannerView alloc] init];
}
_bannerView.delegate = self;
[self.view addSubview:_bannerView];
if (_bannerView==nil) {
NSLog(@"ad view is nil");
}
//place iAd
CGRect contentFrame = self.view.bounds;
// all we need to do is ask the banner for a size that fits into the layout area we are using
CGSize sizeForBanner = [_bannerView sizeThatFits:contentFrame.size];
// compute the ad banner frame
CGRect bannerFrame = _bannerView.frame;
NSLog(@"_bannerView.bannerLoaded %f",_bannerView.bannerLoaded);
if (_bannerView.bannerLoaded) {
// bring the ad into view
contentFrame.size.height -= sizeForBanner.height; // shrink down content frame to fit the banner below it
bannerFrame.origin.y = contentFrame.size.height;
bannerFrame.size.height = sizeForBanner.height;
bannerFrame.size.width = sizeForBanner.width;
// if the ad is available and loaded, shrink down the content frame to fit the banner below it,
// we do this by modifying the vertical bottom constraint constant to equal the banner's height
//
NSLayoutConstraint *verticalBottomConstraint = self.bottomConstraint;
verticalBottomConstraint.constant = sizeForBanner.height;
NSLog(@"verticalBottomConstraint.constant:%f",verticalBottomConstraint.constant);
[self.view layoutSubviews];
} else {
// hide the banner off screen further off the bottom
bannerFrame.origin.y = contentFrame.size.height;
NSLog(@"no iAd");
}
[UIView animateWithDuration:0.25 animations:^{
[self.view layoutIfNeeded];
_bannerView.frame = bannerFrame;
}];
}
- (void)bannerView:(ADBannerView *)banner
didFailToReceiveAdWithError:(NSError *)error
{
NSLog(@"error:%@",error);
}
答案 0 :(得分:0)
我认为,如果初始化_bannerView,则未加载,bannerLoaded
属性为NO
的原因。
最好你可以在ADBannerView
的委托方法上执行隐藏和显示横幅视图,例如在你的情况下
- (void)load_iAd
{
//init iAd
// On iOS 6 ADBannerView introduces a new initializer, use it when available.
if ([ADBannerView instancesRespondToSelector:@selector(initWithAdType:)]) {
_bannerView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
} else {
_bannerView = [[ADBannerView alloc] init];
}
_bannerView.delegate = self;
[self.view addSubview:_bannerView];
if (_bannerView==nil) {
NSLog(@"ad view is nil");
}
}
//in the delegate method of ADBannerView
//int this delegate method better not show banner
- (void)bannerViewWillLoadAd:(ADBannerView *)banner
{
_bannerView.hidden = YES;
NSLog(@"banner view will loaded");
}
//this is the rite time to show banner view and animations to show banner view
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
_bannerView.hidden = NO;
//place iAd
CGRect contentFrame = self.view.bounds;
// all we need to do is ask the banner for a size that fits into the layout area we are using
CGSize sizeForBanner = [_bannerView sizeThatFits:contentFrame.size];
// compute the ad banner frame
CGRect bannerFrame = _bannerView.frame;
// bring the ad into view
contentFrame.size.height -= sizeForBanner.height; // shrink down content frame to fit the banner below it
bannerFrame.origin.y = contentFrame.size.height;
bannerFrame.size.height = sizeForBanner.height;
bannerFrame.size.width = sizeForBanner.width;
// if the ad is available and loaded, shrink down the content frame to fit the banner below it,
// we do this by modifying the vertical bottom constraint constant to equal the banner's height
//
NSLayoutConstraint *verticalBottomConstraint = self.bottomConstraint;
verticalBottomConstraint.constant = sizeForBanner.height;
NSLog(@"verticalBottomConstraint.constant:%f",verticalBottomConstraint.constant);
[self.view layoutSubviews];
[UIView animateWithDuration:0.25 animations:^{
[self.view layoutIfNeeded];
_bannerView.frame = bannerFrame;
}];
}
//int this delegate method better hide the banner view
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
_bannerView.hidden = YES;
NSLog(@"banner view did't load");
//hear goes hide animations for banner view
//....code to hide banner
}
并且我还建议使用横幅视图的共享对象,如果您在应用程序中的某些其他视图控制器中使用横幅,它将使用完整....
希望这有助于你......:)