如何正确隐藏这些广告横幅? [英] How to properly hide these ad banners?

查看:33
本文介绍了如何正确隐藏这些广告横幅?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(Sprite Kit 游戏)我希望在游戏过程中隐藏我的广告横幅.我已将我的项目设置为同时包含 iAd 和 AdMob 广告横幅.在添加 AdMob SDK 和 AdMob 广告代码之前,当我想要隐藏 iAd 横幅时,我没有遇到任何问题.现在有一个问题,因为我的代码是如何设置的,我似乎无法修复它:

(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:

这是代码:

    - (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];
}

如您所见,问题在于隐藏和显示广告方法.它只是同时显示两个广告.当我想显示和隐藏广告时,我不知道如何保持补充.当我不必为特定场景隐藏广告时,补充效果很好(当 iAd 不可用时出现 AdMob),所以这些方法肯定有问题.我想像这样编辑它们:

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];
}

为了防止它们发生冲突.我认为这只会退回到 pragma mark iAd 委托并补充谷歌广告.它没有用.

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.

我怎样才能做到,以便我可以告诉两个广告它们需要在场景中的某个时间展示,同时还要进行补充?有什么建议吗?

推荐答案

您可以使用 BOOL 作为开关来显示 appleAd 或 googleBanner,或者根据需要不显示:

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

在您的 .h 文件中:

In your .h file:

BOOL isAppleAd;
BOOL isGoogleAd;

然后做这样的事情:

- (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];

}

这篇关于如何正确隐藏这些广告横幅?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆