user2052886
user2052886

Reputation: 73

Hide iAd banner when no ads are available

This is a Phonegap app. I am completely lost here, please help. I followed this tutorial to the t.

Here is my MainViewController.m:

- (void)webViewDidFinishLoad:(UIWebView*)theWebView
{
    adView = [[ADBannerView alloc] initWithFrame:CGRectZero];


    if([UIApplication sharedApplication].statusBarOrientation ==
       UIInterfaceOrientationPortrait ||
       [UIApplication sharedApplication].statusBarOrientation ==
       UIInterfaceOrientationPortraitUpsideDown) {
        adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
    }
    else {
        adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
    }


    adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
    CGRect adFrame = adView.frame;
    adFrame.origin.y = self.view.frame.size.height-adView.frame.size.height;
    adView.frame = adFrame;
    [self.view addSubview:adView];

    // Black base color for background matches the native apps
    theWebView.backgroundColor = [UIColor blackColor];

    return [super webViewDidFinishLoad:theWebView];
}

Upvotes: 2

Views: 2467

Answers (1)

Chris C
Chris C

Reputation: 3251

Fast answer: What you need to do is conform to the ADBannerViewDelegate protocol and hide the ad when it fails to receive an ad.

Step by step version:

In that method you listed, you'll want to also include adView.delegate = self.

In MainViewController.h, where it says @interface MainViewController : UIViewController, you want to add <ADBannerViewDelegate> after, like this:

@interface MainViewController : UIViewController <ADBannerViewDelegate>. If there is already something in < > there, just add a comma and add ADBannerViewDelegate.

Back in the MainViewController.m, add these methods:

- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
   adView.hidden = NO;
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
   adView.hidden = YES;
}

Upvotes: 8

Related Questions