在iOS上使用共享横幅切换选项卡时显示AdMob和/或iAd
我有一个带有在iOS上使用共享横幅切换选项卡时显示AdMob和/或iAd,ios,objective-c,admob,iad,Ios,Objective C,Admob,Iad,我有一个带有uitabarcontroller的应用程序,其中包含5个选项卡,每个选项卡都是一个UIViewController,其中嵌入了UITableView。我将IAD和AdMobs带到我的应用程序中,这些应用程序将使用IAP删除。这是一款通用的iPhone和iPad应用程序 起初,我使用共享横幅和AppDelegate实现了iAds,效果非常好。现在,在发布之前,我还将使用AdMob横幅作为备用,以防iAd横幅无法加载。我已经用与iAd横幅相同的方式设置了它 以同样的方式实现实际的AdM
uitabarcontroller
的应用程序,其中包含5个选项卡,每个选项卡都是一个UIViewController
,其中嵌入了UITableView
。我将IAD和AdMobs带到我的应用程序中,这些应用程序将使用IAP删除。这是一款通用的iPhone和iPad应用程序
起初,我使用共享横幅和AppDelegate
实现了iAds,效果非常好。现在,在发布之前,我还将使用AdMob横幅作为备用,以防iAd横幅无法加载。我已经用与iAd横幅相同的方式设置了它
以同样的方式实现实际的AdMob横幅不是一个问题,但我在更改选项卡时面临一个问题
问题
如果加载iAd横幅,并且我从第一个选项卡移动到第二个选项卡,它将继续显示该iAd横幅。如果加载AdMob横幅,并且我从第一个选项卡移动到第二个选项卡,AdMob横幅将消失,直到再次加载
代码:
委托方法:
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
NSLog(@"bannerViewDidLoadAd gets called");
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"IAPSuccessful"])
{
NSLog(@"bannerViewDidLoadAd gets called and the IAP is not successful, so we hide the AdMob and show the iAd");
self.adMobBannerView.hidden = YES;
self.adBanner.hidden = NO;
}
else
{
NSLog(@"bannerViewDidLoadAd gets called and the IAP IS Successful so we hide the AdMob and iAd");
self.adMobBannerView.hidden = YES;
self.adBanner.hidden = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
NSLog(@"The didFailToReceiveAdWithError is called and it is unable to show ads in Timeline. Error: %@ %@", [error localizedDescription], [error domain]);
self.adBanner.hidden = true;
[self displayAdMobBannerOrNot];
}
- (void)displayAdMobBannerOrNot
{
NSLog(@"DisplayAdMobBannerOrNot is called");
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"IAPSuccessful"])
{
NSLog(@"The DisplayAdMonBannerOrNot is called and the IAP is not Successful");
self.adMobBannerView.hidden = NO;
self.adMobBannerView = [[self appdelegate] adMobBanners];
self.adMobBannerView.rootViewController = self;
self.adMobBannerView.delegate = self;
self.adMobBannerView.adUnitID = @"ca-app-pub-394025609333333333335716";
if (IDIOM == IPAD)
{
NSLog(@"The DisplayAdMobBannerOrNot is called and we are using an iPad");
[self.adMobBannerView setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-80, 320, 50)];
[self.adMobBannerView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:self.adMobBannerView];
NSLayoutConstraint *myConstraint =[NSLayoutConstraint
constraintWithItem:self.adMobBannerView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0];
[self.view addConstraint:myConstraint];
myConstraint =[NSLayoutConstraint constraintWithItem:self.adMobBannerView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:0];
[self.view addConstraint:myConstraint];
myConstraint =[NSLayoutConstraint constraintWithItem:self.adMobBannerView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1
constant:0];
[self.view addConstraint:myConstraint];
}
else
{
NSLog(@"The DisplayAdMobBannerOrNot is called and we are using an iPhone");
[self.adMobBannerView setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-98, 414, 50)];
[self.view addSubview:self.adMobBannerView];
GADRequest *request = [GADRequest request];
request.testDevices = @[ @"151111111111ffb836f4d823ac" ];
[self.adMobBannerView loadRequest:request];
}
}
else
{
NSLog(@"The DisplayAdMobBannerOrNot is called and the IAP IS Successful so we'll just hide the iAd and the adMobBanner");
self.adBanner.hidden = YES;
self.adMobBannerView.hidden = YES;
}
}
因此,当调用视图时(每次我回到这个UIViewController
),我都会检查iapsucessful
BOOL
是否为真。如果为false,则加载displayiAdsOrNot
方法。如果失败,它的委托将被调用,调用displayAdMobBannerOrNot
现在,我完全明白了为什么当我显示AdMob横幅并从一个视图移动到另一个视图时,它会删除AdMob横幅,因为当我回来时,视图将出现
为iAd加载共享横幅,而不是AdMob
考虑到这一点,我真的不确定我需要做什么。我希望确保AdMob在每次加载共享横幅时都在加载该横幅。因此,我将共享横幅代码从displayAdMobBannerOrNot
放入displayiAdOrNot
中,它没有改变行为,因为它没有调用功能来实际放置广告(displayAdMobBannerOrNot
)
作为测试,在视图中将出现,当iapsusccessful
为false时,我调用[self bannerView:self.adBanner didfailtoreceiveadwhitherror:nil]代码>而不是其他任何东西,这是有效的。当我从选项卡1移动到选项卡2时,它保持AdMob横幅的显示。然而,这当然不是生产代码。我只是看不清楚 您正在displayiAdsOrNot
函数中隐藏您的adMobBannerView
。每次出现视图时都会调用此函数。去掉这个,你就会得到你想要的结果
至于剩下的代码,这里有很多内容。首先,为了简化工作,当[[NSUserDefaults standardUserDefaults]boolforky:@“iapsusccessful”]
为false时,您应该将创建和布局您的adBanner
和adMobBannerView
的所有代码移动到视图下,并自动设置这两个横幅.hidden=YES
。这将消除对displayiAdsOrNot
和displayAdMobBannerOrNot
的需要,您将重复许多相同的if
语句来检查IAP和用户正在使用的设备。完成此操作后,在委托方法中,您只需隐藏或显示正确的横幅,具体取决于iAd现在是否失败。例如,iAd加载时将其隐藏值设置为NO
,将AdMob设置为YES
,反之亦然,如果iAd无法加载广告。我相信你每次都在检查IAP,以防用户在当前会话中购买了它,这样你就可以删除广告。一个更简单的解决方案是在IAP完成后将广告移到屏幕边界之外,然后在下一次启动应用程序时根本不创建广告如果[[NSUserDefaults standardUserDefaults]boolforky:@“iapsusccessful”]
为true,则任何一个横幅都是正确的。
需要指出的其他几点是,您应该在提交应用程序之前删除您的请求。testDevices
AdMob请求,并且您似乎多次定义了您的NSLayoutConstraint*myConstraint
。我不太清楚你想在这里做什么。此外,在didfailtoreceiveadwhitherror
中,您有self.adBanner.hidden=true
,它应该是self.adBanner.hidden=YES
您应该从此问题中删除您的AdMob发布ID。谢谢Daniel-它只是用随机数填充,这是我使用的教程中的AdMob unitID。嗨,Daniel。非常感谢你的回答,第一部分确实满足了我的要求。另外,我感谢您为代码清理建议所做的额外努力,这对于简化这一过程非常有意义。有了这些约束,我实际上是想将adBanner绑定到AutoLayout的底部、前导和尾随属性。我不确定我是否用代码实现了这一点,但它在测试期间可以在设备上工作。
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
NSLog(@"bannerViewDidLoadAd gets called");
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"IAPSuccessful"])
{
NSLog(@"bannerViewDidLoadAd gets called and the IAP is not successful, so we hide the AdMob and show the iAd");
self.adMobBannerView.hidden = YES;
self.adBanner.hidden = NO;
}
else
{
NSLog(@"bannerViewDidLoadAd gets called and the IAP IS Successful so we hide the AdMob and iAd");
self.adMobBannerView.hidden = YES;
self.adBanner.hidden = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
NSLog(@"The didFailToReceiveAdWithError is called and it is unable to show ads in Timeline. Error: %@ %@", [error localizedDescription], [error domain]);
self.adBanner.hidden = true;
[self displayAdMobBannerOrNot];
}
- (void)displayAdMobBannerOrNot
{
NSLog(@"DisplayAdMobBannerOrNot is called");
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"IAPSuccessful"])
{
NSLog(@"The DisplayAdMonBannerOrNot is called and the IAP is not Successful");
self.adMobBannerView.hidden = NO;
self.adMobBannerView = [[self appdelegate] adMobBanners];
self.adMobBannerView.rootViewController = self;
self.adMobBannerView.delegate = self;
self.adMobBannerView.adUnitID = @"ca-app-pub-394025609333333333335716";
if (IDIOM == IPAD)
{
NSLog(@"The DisplayAdMobBannerOrNot is called and we are using an iPad");
[self.adMobBannerView setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-80, 320, 50)];
[self.adMobBannerView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:self.adMobBannerView];
NSLayoutConstraint *myConstraint =[NSLayoutConstraint
constraintWithItem:self.adMobBannerView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0];
[self.view addConstraint:myConstraint];
myConstraint =[NSLayoutConstraint constraintWithItem:self.adMobBannerView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:0];
[self.view addConstraint:myConstraint];
myConstraint =[NSLayoutConstraint constraintWithItem:self.adMobBannerView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1
constant:0];
[self.view addConstraint:myConstraint];
}
else
{
NSLog(@"The DisplayAdMobBannerOrNot is called and we are using an iPhone");
[self.adMobBannerView setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-98, 414, 50)];
[self.view addSubview:self.adMobBannerView];
GADRequest *request = [GADRequest request];
request.testDevices = @[ @"151111111111ffb836f4d823ac" ];
[self.adMobBannerView loadRequest:request];
}
}
else
{
NSLog(@"The DisplayAdMobBannerOrNot is called and the IAP IS Successful so we'll just hide the iAd and the adMobBanner");
self.adBanner.hidden = YES;
self.adMobBannerView.hidden = YES;
}
}