ADBannerView Confusion (iOS 8)

293 views Asked by At

I've searched and searched and searched and searched for clarification on how to do this, but I still don't know how to do this correctly.

My predicament and what I want my result to be:

I have an ADBannerView added to my storyboard, and the variable name where I implement it is bannerView. I'm making an option to disable ads in the game. If ads are disabled, then ads shouldn't even load. If I'm correct, then the delegate's bannerViewWillLoadWithAd: method shouldn't be called, nor should the bannerViewDidLoadAd: method. I use this code, enclosed in an "if" statement, to remove the ADBannerView from the view controller:

[bannerView removeFromSuperview];
[self setCanDisplayBannerAds:NO];

And then my delegate methods look like this (my delegate is my game scene, and the view controller is referenced by a property of said scene viewController1):

-(BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave {
    NSLog(@"banner view action will begin.");
    self.paused = YES;
    return YES;
}

-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
    NSLog(@"Ad loaded.");
}

-(void)bannerViewWillLoadAd:(ADBannerView *)banner {
    NSLog(@"Ad Banner will load ad.");
    if (// ads are disabled) {
        viewController1.canDisplayBannerAds = NO;
        [banner removeFromSuperview]; 
        NSLog(@"Banner shouldn't load");
    }
}

-(void)bannerViewActionDidFinish:(ADBannerView *)banner{
    NSLog(@"Ad Banner action did finish");
    self.paused = NO;
}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
    NSLog(@"Ad banner view failed to load. Details about the error: %@", error.debugDescription);
    banner.hidden = YES;
}

The code works, but I end up getting errors and odd happenings in the logs, which typically include "service session terminated" when ads are supposed to show and ads loading when they're not supposed to. Any idea how to fix this?

1

There are 1 answers

5
Daniel Storm On BEST ANSWER

First, you're using [self setCanDisplayBannerAds:YES] in addition to creating your own ADBannerView. You need to use one or the other. [self setCanDisplayBannerAds:YES] is actually creating an ADBannerView for you in addition to the one you are creating.

To remove the ads you should not be waiting until an ad loads to deal with hiding them. You should check once at the launch of your application and deal with it then. If you decide to use setCanDisplayBannerAds its quite simple:

-(void)viewDidLoad {
    [super viewDidLoad];
    if (disableAds) {
        self.canDisplayBannerAds = NO;
    }
}

If you decide to use your own implemented ADBannerView your code may look more like this:

-(void)viewDidLoad {
    [super viewDidLoad];
    if (disableAds) {
        banner.hidden = YES;
        banner.delegate = nil;
    }
}