OGeek|极客世界-中国程序员成长平台

标题: ios - 在 iOS 上使用共享横幅切换标签时显示 AdMob 和/或 iAd [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 06:38
标题: ios - 在 iOS 上使用共享横幅切换标签时显示 AdMob 和/或 iAd

我有一个带有 UITabBarController 的应用程序,其中包含 5 个选项卡,其中每个选项卡都是一个 UIViewController,其中嵌入了一个 UITableView。我带来了iAds 和 AdMobs 到我的应用程序,将使用 IAP 删除。这是一个通用的 iPhone 和 iPad 应用程序。

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

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

问题

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

代码:

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

}

委托(delegate)方法:

- (void)bannerViewDidLoadAdADBannerView *)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)bannerViewADBannerView *)banner didFailToReceiveAdWithErrorNSError *)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 方法。如果失败,它的委托(delegate)将被调用,它调用 displayAdMobBannerOrNot

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

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

作为测试,在viewWillAppear中,当IAPSuccessful为false时,我调用了[self bannerView:self.adBanner didFailToReceiveAdWithError:nil]; 而不是其他任何东西,并且有效。当我从标签一移动到标签二时,它会一直显示 AdMob 横幅。但是,这当然不是生产代码。我只是无法清楚地看到这一点。



Best Answer-推荐答案


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

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

关于ios - 在 iOS 上使用共享横幅切换标签时显示 AdMob 和/或 iAd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29891026/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4