2

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?

DDPWNAGE
  • 1,423
  • 10
  • 37

1 Answers1

0

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;
    }
}
Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
  • I never set `canDisplayBannerAds` to `YES`. However, I can remove where it sets `canDisplayBannerAds` to `NO`. – DDPWNAGE Jun 12 '15 at 12:36
  • Sorry @DDPWNAGE, I assumed you were setting `canDisplayBannerAds` as you showed you were setting it to `NO`. There's no need to set it to `NO` if you're not setting it to `YES`. Simply remove `canDisplayBannerAds` and follow the second example I've provided. – Daniel Storm Jun 13 '15 at 12:36
  • I will when I get the chance. I didn't know `canDisplayBannerAds` would make a second banner view on the bottom of the screen. How would one get properties of _that_ banner view (like the delegate, the frame, etc.)? – DDPWNAGE Jun 13 '15 at 12:38
  • @DDPWNAGE from my experience it is not possible. You could check your `view` to see if it contains an `ADBannerView` or not and respond accordingly, but If you want complete and easy control implement your own `ADBannerView` like you have done already. – Daniel Storm Jun 13 '15 at 12:47
  • 1
    alright. I was curious about that. It's weird for me to create a banner view with no delegate object. I'm going to implement my own banner view. – DDPWNAGE Jun 13 '15 at 12:48