Liam Stockhomme
Liam Stockhomme

Reputation: 523

How to properly hide these ad banners?

(Sprite Kit Game) I want my ad banners to be hidden during gameplay. I've set up my project to contain both iAd and AdMob advertisement banners. Prior to adding in the AdMob SDK and the code for the AdMob advertisements, I had no problem with hiding the iAd banner when I wanted it hidden. Now there is a problem because of how my code is set up and I can't seem to fix it:

This is the code:

    - (void)viewDidLoad
{
    [super viewDidLoad];

    //Add view controller as observer
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"hideAd" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"showAd" object:nil];

    // Present the scene.
    [skView presentScene:scene];
    self.canDisplayBannerAds = YES;

    appleAd = [[ADBannerView alloc] initWithFrame:CGRectZero];
    appleAd.frame = CGRectOffset(appleAd.frame, 0, 0.0f);
    appleAd.delegate = self;
    //hide the apple ad so it appears when told to 
    appleAd.alpha = 0;
    [self.view addSubview:appleAd];

    //google ad
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        googleBanner_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeLeaderboard origin:CGPointMake(20, 0)];
    }else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        googleBanner_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner origin:CGPointMake(0, 100)];
    }
    googleBanner_.adUnitID = @"•••••••••••••••••••••••••pub";
    googleBanner_.rootViewController = self;
    [self.view addSubview:googleBanner_];

    [googleBanner_ loadRequest:[GADRequest request]];

    GADRequest *request = [GADRequest request];
    request.testDevices = @[ @"•••••••••••••••••••••••" ];

    //hide the google advertisement when it loads because prioritising iAd and so it appears when told to 
    googleBanner_.alpha = 0;

}

-(void)handleNotification:(NSNotification *)notification {
    if ([notification.name isEqualToString:@"hideAd"]) {
        [self hidesBanner];
    }else if ([notification.name isEqualToString:@"showAd"]){
        [self showsBanner];
    }
}

//THIS IS WHERE THE ISSUES ARE: 
-(void)showsBanner {
    NSLog(@"Showing Banner");
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    [appleAd setAlpha:1];
    [UIView commitAnimations];

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    [googleBanner_ setAlpha:1];
    [UIView commitAnimations];

    if (appleAd.alpha == 1) {
        googleBanner_.alpha = 0;
        NSLog(@"google banner is hidden");
    }
}
-(void)hidesBanner{
    NSLog(@"Hiding Banner");
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0];
    [appleAd setAlpha:0];
    [UIView commitAnimations];

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0];
    [googleBanner_ setAlpha:0];
    [UIView commitAnimations];

    if (appleAd.alpha == 0) {
        googleBanner_.alpha = 1.0;
        NSLog(@"google banner is showing");
    }
}

//iAd delegate
#pragma mark iAd Delegate Methods

-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
    //iAd
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    [appleAd setAlpha:1];
    [UIView commitAnimations];

    //googleAd
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0];
    [googleBanner_ setAlpha:0];
    [UIView commitAnimations];
}

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

    //googleAd
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    [googleBanner_ setAlpha:1.0];
    [UIView commitAnimations];
}

As you can see the problem is inside the hide and show ad methods. It simply shows both advertisements at the same time. I'm not sure how to keep the supplementation going when I want to show and hide the ad. When I don't have to hide the ad for specific scenes, the supplementation works fine (AdMob appears when iAd is unavailable), so it's definitely a problem with those methods. I thought to edit them like this:

 -(void)showsBanner {
    NSLog(@"Showing Banner");
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    [appleAd setAlpha:1];
    [UIView commitAnimations];
}
-(void)hidesBanner{
    NSLog(@"Hiding Banner");
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0];
    [appleAd setAlpha:0];
    [UIView commitAnimations];
}

To prevent them from conflicting. I assumed this would just fall back to the pragma mark iAd delegate and supplement the google ad. It didn't work.

How can I make it so that I can tell both ads they need to be shown at a certain time in a scene, but also have the supplementation occur? Any suggestions?

Upvotes: 2

Views: 710

Answers (1)

l'L'l
l'L'l

Reputation: 47169

You could use a BOOL as a switch to show either appleAd or googleBanner, or none as you might want:

In your .h file:

BOOL isAppleAd;
BOOL isGoogleAd;

Then do something like this:

- (void)showsBanner {

    if (isAppleAd == YES) {
        [self appleAd];
    }
    if (isGoogleAd == YES) {
        [self googleAd];
    }
    else {
        [self hideBothBanners];
    }

}

- (void)appleAd {

        if (isAppleAd == YES) {

        NSLog(@"Showing Apple Banner");

        //googleAd OFF
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0];
        [googleBanner_ setAlpha:0];
        [UIView commitAnimations];

        // iAd ON
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1.0];
        [appleAd setAlpha:1.0];
        [UIView commitAnimations];

        // switch off AppleAd to use as switch
        isAppleAd = NO;
        isGoogleAd = YES;

    } else {
        // do something else
        return;
    }

}

- (void)googleAd {

    if (isGoogleAd == YES) {

        NSLog(@"Showing Google Banner");

        // iAd OFF
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0];
        [appleAd setAlpha:0];
        [UIView commitAnimations];

        // googleAd ON
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1.0];
        [googleBanner_ setAlpha:1.0];
        [UIView commitAnimations];

        // switch off GoogleAd to use as switch
        isGoogleAd = NO;
        isAppleAd = YES;

    } else {
        // do something else
        return;
    }

}

- (void)hideBothBanners {

        NSLog(@"Hiding Both Banners");

        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0];
        [appleAd setAlpha:0];
        [googleBanner_ setAlpha:0]
        [UIView commitAnimations];

}

Upvotes: 3

Related Questions