开发者

When my application first launches, iAd refuses to load an ad on the first screen

When my application launches, iAd refuses to load an ad on the first screen. No error message, nothing. If I switch screens (going to another screen), it starts getting ads and serving them up, even when I go back to the first screen.

I am using a single iAd instance that is in the ApplicationDelegate. I am attempting to link in the iAdBanner in viewDidAppear, and unlink in viewWillDisappear.

The viewDidAppear method:

- (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];
}

The layout method:

- (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) ? ADBannerContentSizeIdentifierLandsc开发者_StackOverflow社区ape : 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);
                     }];
}

and the viewWillDisappear method:

-(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];
    }
}

The real frustration was this was working in the simulator before I upgraded to xcode 4. I upgraded, and all of a sudden it has stopped working. Anyone else seen behavior like this? Any ideas what I can do to fix it? The behavior occurs in the simulator on all test versions of 4.x (4.0 through 4.3).


I have encountered the same problem, and the fix is in the AppDelegate's bannerViewDidLoadAd method. You need to call your showBanner method there in order for the ad to display initially.

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

Please check the new iAdSuite sample codes where I took this out.

iAdSuite Sample Code Updated 10/31/11

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜