gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:05:40 26 4
gpt4 key购买 nike

(Sprite Kit 游戏)我希望在游戏过程中隐藏我的广告横幅。我已将我的项目设置为同时包含 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];
}

防止它们冲突。我认为这只会退回到 pragma mark iAd delegate 并补充谷歌广告。它没有用。

我怎样做才能告诉两个广告它们需要在场景中的特定时间显示,而且还要进行补充?有什么建议吗?

最佳答案

您可以使用 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];

}

关于ios - 如何正确隐藏这些广告横幅?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23049110/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com