当我的应用程序首次启动时,iAd拒绝在第一个屏幕上加载广告

时间:2011-05-07 00:45:41

标签: ios 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)的所有测试版本的模拟器中。

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,修复程序位于AppDelegate的bannerViewDidLoadAd方法中。您需要在那里调用showBanner方法,以便最初显示广告。

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
     [_currentController showBannerView:_bannerView animated:YES];
}

请查看我拍摄的新iAdSuite示例代码。

iAdSuite Sample Code Updated 10/31/11