简体   繁体   English

ADBannerView混淆(iOS 8)

[英]ADBannerView Confusion (iOS 8)

I've searched and searched and searched and searched for clarification on how to do this, but I still don't know how to do this correctly. 我已经搜索,搜索,搜索了有关如何执行此操作的说明,但是我仍然不知道如何正确执行此操作。

My predicament and what I want my result to be: 我的困境以及我想要的结果是:

I have an ADBannerView added to my storyboard, and the variable name where I implement it is bannerView . 我在情节ADBannerView添加了一个ADBannerView ,实现它的变量名称是bannerView I'm making an option to disable ads in the game. 我可以选择禁用游戏中的广告。 If ads are disabled, then ads shouldn't even load. 如果广告被禁用,则广告甚至都不会加载。 If I'm correct, then the delegate's bannerViewWillLoadWithAd: method shouldn't be called, nor should the bannerViewDidLoadAd: method. 如果我是正确的,则不应调用委托的bannerViewWillLoadWithAd:方法,也不应调用bannerViewDidLoadAd:方法。 I use this code, enclosed in an "if" statement, to remove the ADBannerView from the view controller: 我使用括在“ if”语句中的以下ADBannerView从视图控制器中删除ADBannerView

[bannerView removeFromSuperview];
[self setCanDisplayBannerAds:NO];

And then my delegate methods look like this (my delegate is my game scene, and the view controller is referenced by a property of said scene viewController1 ): 然后我的委托方法如下所示(我的委托是我的游戏场景,并且视图场景的viewController1的属性引用了视图控制器):

-(BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave {
    NSLog(@"banner view action will begin.");
    self.paused = YES;
    return YES;
}

-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
    NSLog(@"Ad loaded.");
}

-(void)bannerViewWillLoadAd:(ADBannerView *)banner {
    NSLog(@"Ad Banner will load ad.");
    if (// ads are disabled) {
        viewController1.canDisplayBannerAds = NO;
        [banner removeFromSuperview]; 
        NSLog(@"Banner shouldn't load");
    }
}

-(void)bannerViewActionDidFinish:(ADBannerView *)banner{
    NSLog(@"Ad Banner action did finish");
    self.paused = NO;
}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
    NSLog(@"Ad banner view failed to load. Details about the error: %@", error.debugDescription);
    banner.hidden = YES;
}

The code works, but I end up getting errors and odd happenings in the logs, which typically include "service session terminated" when ads are supposed to show and ads loading when they're not supposed to. 该代码有效,但最终在日志中出现错误和奇怪的情况,通常包括当应该显示广告时“服务会话终止”,而在不应该显示广告时加载广告。 Any idea how to fix this? 任何想法如何解决这个问题?

First, you're using [self setCanDisplayBannerAds:YES] in addition to creating your own ADBannerView . 首先,除了创建自己的ADBannerView之外,还要使用[self setCanDisplayBannerAds:YES] You need to use one or the other. 您需要使用其中一个。 [self setCanDisplayBannerAds:YES] is actually creating an ADBannerView for you in addition to the one you are creating. [self setCanDisplayBannerAds:YES]实际上是创建一个ADBannerView除了创建一个给你。

To remove the ads you should not be waiting until an ad loads to deal with hiding them. 要删除广告,您无需等到广告加载后就可以对其进行隐藏。 You should check once at the launch of your application and deal with it then. 您应该在启动应用程序时检查一次,然后再进行处理。 If you decide to use setCanDisplayBannerAds its quite simple: 如果您决定使用setCanDisplayBannerAds非常简单:

-(void)viewDidLoad {
    [super viewDidLoad];
    if (disableAds) {
        self.canDisplayBannerAds = NO;
    }
}

If you decide to use your own implemented ADBannerView your code may look more like this: 如果决定使用自己实现的ADBannerView代码可能看起来像这样:

-(void)viewDidLoad {
    [super viewDidLoad];
    if (disableAds) {
        banner.hidden = YES;
        banner.delegate = nil;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM