【问题标题】:Removing an AdBannerView from a UIViewController from an IAP without having to restart the app从 IAP 的 UIViewController 中删除 AdBannerView,而无需重新启动应用程序
【发布时间】:2023-03-10 09:08:02
【问题描述】:

我有一个 5 选项卡 UITabBarController,其中每个选项卡都是一个 UIViewController。第一个选项卡称为Timeline,第二个选项卡称为Person,最后一个选项卡称为More

我已经在我的应用程序中实现了In-App Purchases,在IAP 之前,用户无法更改主题等。我为这个版本带来的一件事是使用IAP 删除iAds .

所以Timeline 在创建IAP 之前在底部有一个AdBannerView,并且一旦从More 标签购买IAP,我期待@987654336 @ 立即从Timeline 中删除。

问题

现在实际发生的是我转到More 选项卡,购买IAP 并返回TimelineAdBannerView 仍然存在。如果我从Timeline 选项卡移动到Person 选项卡并返回Timeline 选项卡,AdBannerView 将被删除。或者,如果我在IAP 之后重新启动应用程序,则AdBannerView 将从Timeline 中删除。无论哪种方式,它都没有达到我的预期,即在我第一次从More 选项卡返回Timeline 选项卡时,在IAP 之后删除AdBannerView

这里有一些代码:

- (void)displayiAdsOrNot
{
    NSLog(@"Display iAds or Not");
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"IAPSuccessful"])
    {
        NSLog(@"BASIC");

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

    }
    else
    {
        NSLog(@"PRO");
        [self.adBanner removeFromSuperview];
        self.adBanner = nil;
    }   
}

这个方法只能从viewWillAppear 调用。

NSLogs 为指导,在Timeline 选项卡中,控制台显示BASIC。当我转到More 选项卡,购买IAP 并返回Timeline 选项卡时,viewWillAppear 再次触发,从而触发displayiAdsOrNot 方法,这一次,我得到NSLog@ 987654364@ 因为NSUserDefault IAPSuccessful 现在为真(在购买完成时完成)。

但是,此时,AdBannerView 仍会显示。

我的尝试

我已经尝试了很多方法来尝试使其正常工作:

  • 在购买完成时创建一个NSNotification,并在Timeline 选项卡的viewWillAppear 方法中创建一个侦听器。在该方法中,将self.adBanner 设置为nil,将其从superview 中删除,等等。选择器被Notification 触发(在viewWillAppear 之前),但没有发生任何不同。
  • 我试过self.adBanner.delegate = self; self.adBanner = nil; [self.adBanner removeFromSuperview];等,还是一样
  • 我试过[self.timelineTableView reloadData];
  • 遵循这个类似的 SO 问题 (Reloading an SKScene or View to remove iAd after In App Purchase),我什至尝试将 ivar 设置为无效。
  • 我尝试将self.adBanner 置于屏幕外,但同样的事情正在发生;加载时间轴后,AdBannerView 仍然存在,直到我去其他地方。

我的viewWillDisappear如下:

- (void)viewWillDisappear:(BOOL)animated
{
    self.adBanner.delegate = nil;
    self.adBanner=nil;
    [self.adBanner removeFromSuperview];
}

这是为了使用共享AdBannerViewsself.adBanner 是在 .h 文件中创建的属性,我在 .m 文件中使用 @synthesize adBanner = _adBanner

对此的任何指导将不胜感激。

【问题讨论】:

    标签: ios objective-c iphone nsnotification adbannerview


    【解决方案1】:

    我已经解决了这个问题。

    通过删除viewWillDisappear 并在IAPSuccessfulNotification 中设置self.adBanner.hidden 并设置= YES,我已经设法摆脱AdBannerView 而无需重新启动应用程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-22
      • 1970-01-01
      • 2017-12-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-18
      相关资源
      最近更新 更多