Search code examples
iosobjective-cuiviewcontrolleruinavigationcontrolleriad

Shared iAd banner unloads ad after dismissing navigation popover


I saw a few other similar questions but could find any answers... I have a shared iAd banner. The problem is when I load it in a modally presented NavigationController with a TableViewController, when I dismiss the TableViewController the Ad Banner in the MainViewController loses the ad and I receive the following error:

ADBannerView: Unhandled error (no delegate or delegate does not implement didFailToReceiveAdWithError:): Error Domain=ADErrorDomain Code=7 "The operation couldn’t be completed. Ad was unloaded from this banner" UserInfo=0x5b60e283649 {ADInternalErrorCode=7, NSLocalizedFailureReason=Ad was unloaded from this banner, ADInternalErrorDomain=ADErrorDomain}

The View setup us like this: MainViewController --> NavigationController --> TableViewController

At another point in the app I modally present a ViewController with shared ad and I don't experience this issue. (MainViewController --> ViewController)

Here is the code from the TableViewController:

.h

#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#import "AppDelegate.h"

@interface TableViewController : UITableViewController <ADBannerViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *table;
@property (strong, nonatomic) ADBannerView *adView;

@end

.m

-(void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    // Insert Ad Bar
    AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication ]delegate];
    _adView = [appdelegate adView];
    _adView.delegate = self;
    self.canDisplayBannerAds = true;

    if (IPAD) {
        [_adView setFrame:CGRectMake(0, self.view.frame.size.height - 65, 320, 65)];
    }
    else {
        [_adView setFrame:CGRectMake(0, self.view.frame.size.height - 50, 320, 50)];
    }
    if (!_adView.bannerLoaded) {
        [_adView setAlpha:0];
    }
    [self.view addSubview:_adView];
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1];
    [banner setAlpha:1];
    [UIView commitAnimations];
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1];
    [banner setAlpha:0];
    [UIView commitAnimations];
}

Any tips would be appreciated. Thanks


Solution

  • The problem was because I was using:

    self.canDisplayBannerAds = true;
    

    as well as:

    if (IPAD) {
        [_adView setFrame:CGRectMake(0, self.view.frame.size.height - 65, 320, 65)];
    }
    else {
        [_adView setFrame:CGRectMake(0, self.view.frame.size.height - 50, 320, 50)];
    }
    if (!_adView.bannerLoaded) {
        [_adView setAlpha:0];
    }
    [self.view addSubview:_adView];
    

    and thus was created 2 instances of the ad bar.