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

标签 ios objective-c admob iad

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

起初,我只使用共享横幅和 AppDelegate 实现了 iAd,效果非常好。现在,在发布之前,我还将使用 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];
    }

}

委托(delegate)方法:

- (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 方法。如果失败,将调用它的委托(delegate),调用 displayAdMobBannerOrNot

现在,我完全理解为什么当我显示 AdMob 横幅并从一个 View 移动到另一个 View 时,它会删除 AdMob 横幅,因为当我回来时,viewWillAppear 会加载 Shared iAd 而非 AdMob 的横幅。

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

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

最佳答案

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

至于你的其余代码,你在这里有很多事情要做。首先,为了简化事情,当 [ [NSUserDefaults standardUserDefaults] boolForKey:@"IAPSuccessful"] 为 false,并自动设置两个横幅 .hidden = YES。这将消除您对 displayiAdsOrNotdisplayAdMobBannerOrNot 的需要,您在其中重复许多相同的 if 语句来检查 IAP 和用户使用的设备正在使用。完成此操作后,在您的委托(delegate)方法中,您可以简单地隐藏或显示正确的横幅,具体取决于 iAd 现在是否失败。例如,iAd 加载时将其隐藏值设置为 NO,将 AdMob 的隐藏值设置为 YES,如果 iAd 无法加载广告,则反之亦然。我确定您每次都在检查 IAP,以防用户在当前 session 中购买它,这样您就可以删除广告。一个更简单的解决方案是在 IAP 完成时将广告移到屏幕边界之外,然后在您的应用程序的下一次启动时根本不创建任何横幅 if [[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/

相关文章:

ios - imo、whatsapp 和 skype 等 iPhone 锁定时如何实现应用程序调用?

java - Android 广告请求。字符串数组中的关键字

iOS 功能和按钮

ios - UICollectionView 高度的动画看起来很奇怪,单元格缩小

iphone - Titanium - 用于注释的事件监听器

objective-c - objective-c 中的视频编辑框架

ios - 如何在 X 秒后更改 NSTimer 的 NSTimeInterval?

objective-c - Objective C 匿名对象的内存管理

java - com.google.ads.AdView 实例化失败 - java.lang.ClassNotFoundException : org. json.JSONException

android - Admob尚未运行时,将其添加到我现有的项目中