如何正确隐藏这些广告横幅?

时间:2014-04-13 22:15:31

标签: ios objective-c admob sprite-kit iad

(精灵套装游戏)我希望在游戏过程中隐藏我的广告横幅。我已将我的项目设置为包含iAd和AdMob广告横幅。在添加AdMob SDK和AdMob广告代码之前,我隐藏iAd横幅时没有任何问题。现在有一个问题,因为我的代码设置如何,我似乎无法解决它:

这是代码:

    - (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),因此这些方法肯定存在问题。我想像这样编辑它们:

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

防止他们发生冲突。我认为这只会回到pAdma mark iAd代表并补充谷歌广告。它没有用。

我怎样才能这样做,以便我可以告诉他们需要在场景中的特定时间显示两个广告,还有补充发生?有什么建议吗?

1 个答案:

答案 0 :(得分:3)

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

在你的.h文件中:

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

}