【问题标题】:GADInterstitial presentFromRootViewController causes current View Controller to closeGADInterstitial presentFromRootViewController 导致当前视图控制器关闭
【发布时间】:2018-10-26 03:34:55
【问题描述】:

当我的应用程序的用户单击某个按钮转到某个 ViewController 时,我正在尝试显示 GADInterstitial 广告。在新 ViewController 的 viewDidLoad 方法中,我检查 GADInterstitial 广告是否已准备好,如果是,我会尝试展示它。问题是,当我关闭广告时,我注意到展示广告的 ViewController 不再存在,而是将我送回了启动展示广告的 ViewController 的 ViewController。

这是我的代码:

   - (void)viewDidLoad {
[super viewDidLoad];


BTRInterstitialHelper *helper = [BTRInterstitialHelper sharedHelper];
if([helper isInterstitialReady]) {
    [helper.interstitial presentFromRootViewController:self];
}

我的辅助方法看起来像这样:

    -(BOOL)isInterstitialReady {
if ([self.interstitial isReady]) {
    return YES;
} else {
    return NO;
}
 }

我确实注意到,当我展示广告时,此消息会显示在日志中:

  Presenting view controllers on detached view controllers is discouraged BTRIndividualLocationViewController: 0x7fd63bcd9c00.

如果我尝试在 viewWillAppear 中显示,也会出现同样的问题。

我在这里做错了什么?

编辑: 我还尝试了以下方法,以尝试传入我的应用程序的实际 rootViewController:

     UINavigationController *nav=(UINavigationController *)((BTRAppDelegate *)[[UIApplication sharedApplication] delegate]).window.rootViewController;
    UIViewController *rootController=(UIViewController *)[nav.viewControllers objectAtIndex:0];

    [helper.interstitial presentFromRootViewController:rootController];

仍然是相同的结果,除了该消息没有在日志中弹出。

【问题讨论】:

  • 也许只是在 VC 中做所有你想要实际展示的插页式的东西。使用 NSUserdefaults,当用户按下按钮时,将一个值设置为 NSUserdefaults,在 VC 中,您要显示该 NSUserdefaults 的值的插页式检查,如果显示插页式,当它被触发时,下次重置 NSUserdefaults 的值... !?
  • 我的意思是,你甚至不需要 NSUserdefaults 的东西,为什么你在另一个你不想显示的 VC 中启动插页式广告?
  • 您想在每次显示 VC 时都显示插页式广告?或者只有当它来自一个特殊的按钮?如果是这样,请使用 NSUserdefaults,但在要显示它的 VC 中而不是在另一个中执行插页式内容。
  • 我想进入一个 VC (vc1),它上面有一个按钮,可以启动另一个 VC (vc2)。基本上在从 vc1 到 vc2 的过渡中,我希望广告弹出。当您关闭广告时,vc2 应该在那里等待。您在上面看到的所有代码都在vc2中。
  • 是的,VC2 中的所有插页式内容也是如此。你为什么在 VC1 中这样做?

标签: ios uiviewcontroller admob interstitial


【解决方案1】:

VC2.h

#import "GADInterstitial.h"
#import "GADInterstitialDelegate.h"

设置委托

@interface VC2 : UIViewController <GADInterstitialDelegate>

还有一个属性

@property(nonatomic, strong) GADInterstitial *interstitial;

VC2.m 在视图中出现了

self.interstitial = [[GADInterstitial alloc] init];
self.interstitial.adUnitID = @"ca-app-pub-yourNumber";
self.interstitial.delegate = self;

GADRequest *request = [GADRequest request];

用于模拟器中的测试广告

request.testDevices = @[ GAD_SIMULATOR_ID ];

还可以在请求中添加位置和关键字(可选)

然后请求

[self.interstitial loadRequest:request];

听取委托

-(void)interstitialDidReceiveAd:(GADInterstitial *)ad {

if ([self.interstitial isReady]) {


    [self.interstitial presentFromRootViewController:self];
  }
}

如果您只想在按下某个按钮时显示插页式广告,请使用 NSUserdefaults。

按下按钮时在 VC1 中

[[NSUserDefaults standardUserDefaults] setValue:@"yes" forKey:@"showInterstitial"];
[[NSUserDefaults standardUserDefaults] synchronize];

并将 VC2 中插页式广告的所有代码包装在 viewdidappear 中:

if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"showInterstitial"] isEqualToString:@"yes"]) {

}

并添加到 -(void)interstitialDidReceiveAd:(GADInterstitial *)ad {

[[NSUserDefaults standardUserDefaults] setValue:@"no" forKey:@"showInterstitial"];
[[NSUserDefaults standardUserDefaults] synchronize];

附言当然,您也可以为 NSUserdefaults 使用 Bool,我确实使用字符串,因为我在使用 Bools 时遇到了一些错误,但我猜 Bools 会更正确

【讨论】:

  • 我想我的问题一定没有说清楚,因为我认为您没有理解它。我的问题不是仅在某些情况下如何展示广告。我的问题是为什么当我退出广告时我的视图控制器会关闭?
  • 哦,好吧,我想这与您的助手类有关。为什么要使用助手类?您会将其用于共享横幅单例,但使用插页式广告就不需要了!?只需使用我的代码,只需几行代码,它就可以工作......不需要那个助手
  • 并在 viewdidappear 中执行,viewdidload 只加载一次,而不是每次显示视图时
  • 其实,我只是想通了。这是我在自定义 UIPageCotronller 中进行的其他一些时髦的事情,导致它被关闭。感谢您的努力和提示。我仍然会给你接受的答案。
  • 是的,当我开始的时候,我有一个理由认为我会需要它,但我什至不记得那是什么了。我可能会摆脱它。
【解决方案2】:

2018 年对我来说非常相似。

我当前的 ViewController 如下所示:

  ViewController 1
   -> ViewController 2 (via presentViewController:animated:completion)
    -> GADOInterstitialViewController (presentFromRootViewController:)   

当 GADOInterstitialViewController 通过rewardBasedVideoAdDidClose: 关闭时,ViewController 2 也会立即关闭。

我想出了一个解决这个问题的简单方法。只需像这样覆盖 ViewController 2 函数dismissViewControllerAnimated

 - (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion {

    if(self.presentedViewController != nil){
        [self.presentedViewController dismissViewControllerAnimated:flag completion:completion];
    } else {
        [super dismissViewControllerAnimated:flag completion:completion];
    }
}

此代码块检查 ViewController 2 是否具有呈现的视图控制器并关闭它,如果没有,则 ViewController 2 被关闭。

这在我的情况下非常有效:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-03
    • 2019-05-30
    • 2017-07-09
    • 2016-11-27
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多