当我的应用程序启动时,iAd拒绝在第一个屏幕上加载广告。没有错误信息,没有。如果我切换屏幕(转到另一个屏幕),即使我回到第一个屏幕,它也会开始获取广告并提供服务。当我的应用程序第一次启动时,iAd拒绝在第一个屏幕上加载广告
我正在使用ApplicationDelegate中的一个iAd实例。我试图在viewDidAppear中链接iAdBanner,并在viewWillDisappear中取消链接。
的viewDidAppear方法:
- (void)viewDidAppear:(BOOL)animated
{
NSLog(@"view did appear");
[super viewDidAppear:animated];
ADBannerView *adBanner = SharedAdBannerView;
adBanner.requiredContentSizeIdentifiers = (&ADBannerContentSizeIdentifierPortrait != nil) ?
[NSSet setWithObjects:ADBannerContentSizeIdentifierPortrait, ADBannerContentSizeIdentifierLandscape, nil] :
[NSSet setWithObjects:ADBannerContentSizeIdentifier320x50, ADBannerContentSizeIdentifier480x32, nil];
[self.view addSubview:adBanner];
// set the delegate to self, so that we are notified of ad responses
[adBanner setDelegate:self];
isAdShown = [adBanner isBannerLoaded];
[self layoutForCurrentOrientation:animated];
}
布局方法:
- (void)layoutForCurrentOrientation:(BOOL)animated
{
//TODO: this only handles bottom-located elements
ADBannerView *adBanner = SharedAdBannerView;
CGFloat animationDuration = animated ? 0.2f : 0.0f;
// by default content consumes the entire view area
CGRect contentFrame = contentView.bounds;
CGRect owningViewFrame = [self view].bounds;
// the banner still needs to be adjusted further, but this is a reasonable starting point
// the y value will need to be adjusted by the banner height to get the final position
CGPoint bannerOrigin = CGPointMake(CGRectGetMinX(owningViewFrame), CGRectGetMaxY(owningViewFrame));
CGFloat bannerHeight = 0.0f;
// First, setup the banner's content size and adjustment based on the current orientation
if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
adBanner.currentContentSizeIdentifier = (&ADBannerContentSizeIdentifierLandscape != nil) ? ADBannerContentSizeIdentifierLandscape : ADBannerContentSizeIdentifier480x32;
else
adBanner.currentContentSizeIdentifier = (&ADBannerContentSizeIdentifierPortrait != nil) ? ADBannerContentSizeIdentifierPortrait : ADBannerContentSizeIdentifier320x50;
bannerHeight = adBanner.bounds.size.height;
// Depending on if the banner has been loaded, we adjust the content frame and banner location
// to accomodate the ad being on or off screen.
// This layout is for an ad at the bottom of the view.
if (isAdShown)
{
NSLog(@"Banner is loaded");
contentFrame.size.height = owningViewFrame.size.height - bannerHeight;
bannerOrigin.y -= bannerHeight;
}
else
{
NSLog(@"Banner is not loaded");
bannerOrigin.y += bannerHeight;
contentFrame.size.height = owningViewFrame.size.height;
}
NSLog(@"Banner content Frame: (%f, %f), (%f, %f)", bannerOrigin.x, bannerOrigin.y, contentFrame.size.width, contentFrame.size.height);
// And finally animate the changes, running layout for the content view if required.
[UIView animateWithDuration:animationDuration
animations:^{
contentView.frame = contentFrame;
[contentView layoutIfNeeded];
adBanner.frame = CGRectMake(bannerOrigin.x, bannerOrigin.y, adBanner.frame.size.width, adBanner.frame.size.height);
}];
}
和viewWillDisappear方法:
-(void)viewWillDisappear:(BOOL)animated
{
NSLog(@"View will disappear");
[super viewWillDisappear:animated];
[self removeLinkToAdBanner:animated];
}
-(void)removeLinkToAdBanner:(BOOL)animated
{
ADBannerView *adBanner = SharedAdBannerView;
if ([adBanner delegate] == self) {
adBanner.delegate = nil;
[adBanner removeFromSuperview];
}
}
真正的失望是这是在模拟器上工作,我之前升级到xcode 4.我升级了,突然它停止了工作。其他人看到过这样的行为?任何想法我可以做什么来解决它?行为发生在4.x(4.0到4.3)的所有测试版本上的模拟器中。
我也许应该也注意到,我让它坐了几分钟,并没有看到在所有的iAd的委托任何电话,或到主视图任何变化,但此刻我去不同的观点,它开始正确工作。 – aperkins 2011-05-07 00:54:23
如何定义SharedAdBannerView? – 2011-05-07 02:01:00
#define SharedAdBannerView((VNApplicationDelegate *)[[UIApplication sharedApplication] delegate])。adBanner其中VNApplicationDelegate是我的应用程序代理 – aperkins 2011-05-07 02:09:43