在iOS上使用共享横幅切换标签时显示AdMob和/或iAd

时间:2015-04-27 08:45:17

标签: ios objective-c admob iad

我有一个包含5个标签的UITabBarController的应用,其中每个标签都是UIViewController,其中嵌入了UITableView。我正在将iAd和AdMobs带到我的应用中,这将是使用IAP删除。这是一款通用的iPhone和iPad应用程序。

首先,我使用共享横幅和AppDelegate实现了iAds,并且效果非常好。现在,在发布之前,我还将使用AdMob横幅作为后备,以防iAd横幅无法加载。我的设置方式与iAd横幅相同。

以相同的方式实现实际的AdMob横幅不是问题,但在更改标签时我遇到了问题。

问题

如果iAd横幅加载并且我从第一个标签移动到第二个标签,它会继续显示iAd横幅。如果AdMob横幅加载并且我从第一个标签移动到第二个标签,则AdMob横幅会消失,直到再次加载为止。

代码:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"VIEW WILL APPEAR");
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"IAPSuccessful"])
    {
        NSLog(@"View will appear and the IAP is not Successful");
        [self displayiAdsOrNot];
    }
    else
    {
        NSLog(@"View will appear and the IAP IS Successful");
        self.adBanner.hidden = YES;
        self.adMobBannerView.hidden = YES;
    }
}

- (void)displayiAdsOrNot
{
    NSLog(@"Display iAds or Not");

    self.adMobBannerView.hidden = YES;
    self.adBanner = [[self appdelegate] adBanners];
    self.adBanner.delegate = self;

    if (IDIOM == IPAD)
    {
        NSLog(@"***This is the iPad****");
        [self.adBanner setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-80, 320, 50)];
        [self.adBanner setTranslatesAutoresizingMaskIntoConstraints:NO];

        [self.view addSubview:self.adBanner];
        NSLayoutConstraint *myConstraint =[NSLayoutConstraint
                                           constraintWithItem:self.adBanner
                                           attribute:NSLayoutAttributeLeading
                                           relatedBy:NSLayoutRelationEqual
                                           toItem:self.view
                                           attribute:NSLayoutAttributeLeading
                                           multiplier:1.0
                                           constant:0];

        [self.view addConstraint:myConstraint];



        myConstraint =[NSLayoutConstraint constraintWithItem:self.adBanner
                                                   attribute:NSLayoutAttributeTrailing
                                                   relatedBy:NSLayoutRelationEqual
                                                      toItem:self.view
                                                   attribute:NSLayoutAttributeTrailing
                                                  multiplier:1
                                                    constant:0];

        [self.view addConstraint:myConstraint];

        myConstraint =[NSLayoutConstraint constraintWithItem:self.adBanner
                                                   attribute:NSLayoutAttributeBottom
                                                   relatedBy:NSLayoutRelationEqual
                                                      toItem:self.view
                                                   attribute:NSLayoutAttributeBottom
                                                  multiplier:1
                                                    constant:0];

        [self.view addConstraint:myConstraint];

    }
    else
    {
        NSLog(@"*** THIS IS THE IPHONE ***");
        [self.adBanner setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-98, 320, 50)];
        [self.view addSubview:self.adBanner];
    }

}

委托方法:

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

因此,当viewWillAppear被调用时(每次我回到此UIViewController时),我都会检查IAPSuccessful BOOL是否为真。如果它是假的,我会加载displayiAdsOrNot方法。如果失败,将调用它的委托调用displayAdMobBannerOrNot

现在,我完全理解为什么当我显示AdMob横幅并从一个视图移动到另一个视图时,它会移除AdMob横幅,因为当我回来时,viewWillAppear会为iAd加载共享横幅而不是AdMob。

考虑到这一点,我不确定我需要做什么。我想确保AdMob每次加载时都会加载共享横幅广告。因此,我将displayAdMobBannerOrNot中的共享横幅代码放入displayiAdOrNot并且它没有改变行为,因为它没有调用实际放置广告的功能({{ 1}})。

作为测试,在displayAdMobBannerOrNot中,当viewWillAppear为假时,我调用IAPSuccessful而不是其他任何内容,并且有效。当我从标签1移动到标签2时,它会保留AdMob标题。但是,这当然不是生产代码。我只是无法清楚地看到这一点。

1 个答案:

答案 0 :(得分:1)

您正在将adMobBannerView隐藏在displayiAdsOrNot功能中。每次viewWillAppear时都会调用此函数。删除它,你应该得到你想要的结果。

至于你的其余代码,你有很多事情要做。首先,为简化操作,您应移动所有正在创建的代码,并在adBanner为false时在adMobBannerView下展示viewWillAppear[[NSUserDefaults standardUserDefaults] boolForKey:@"IAPSuccessful"],并设置两个横幅{ {1}}自动这将消除您.hidden = YESdisplayiAdsOrNot的需要,您需要重复检查IAP以及用户使用的设备的许多相同displayAdMobBannerOrNot语句。执行此操作后,您可以在委托方法中隐藏或显示正确的横幅,具体取决于iAd现在是否失败。例如,iAd加载,因此将其隐藏值设置为if,将AdMob设置为NO,反之亦然,如果iAd无法加载广告。我确定您每次都在检查IAP,以防用户在当前会话中购买它,以便您可以删除广告。一个更简单的解决方案是在完成IAP后将广告移出屏幕边界,然后在下次启动应用程序时,如果YES为真,则根本不创建任何横幅。 还有一些需要指出的事项是,您应该在提交申请之前删除[[NSUserDefaults standardUserDefaults] boolForKey:@"IAPSuccessful"] AdMob请求,而且您似乎要多次定义request.testDevices次。我不确定你在这里要做什么。此外,在NSLayoutConstraint *myConstraint didFailToReceiveAdWithError,您应该self.adBanner.hidden = true