GADInterstitial presentFromRootViewController导致当前的View Controller关闭

问题描述 投票:0回答:2

当我的应用用户点击某个按钮转到某个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];

但结果仍然相同,除了日志中没有弹出消息。

ios uiviewcontroller admob interstitial
2个回答
1
投票

VC2.h

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

设置代表

@interface VC2 : UIViewController <GADInterstitialDelegate>

和财产

@property(nonatomic, strong) GADInterstitial *interstitial;

VC2.m在viewdid中出现

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

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

然后请求

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

并在viewdid中显示VC2中插页式广告的所有代码:

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

}

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

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

附:你当然也可以使用Bool for NSUserdefaults,我确实使用了字符串caus我在Bools中遇到了一些错误,但Bools会更正确我猜


0
投票

一个非常类似于2018年的情景。

我当前的ViewControllers看起来像这样:

  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将被解除。

这在我的情况下完美地工作:)

© www.soinside.com 2019 - 2024. All rights reserved.