GADInterstitial presentFromRootViewController导致当前的View Controller关闭

时间:2015-01-31 00:10:47

标签: ios uiviewcontroller admob interstitial

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

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

2 个答案:

答案 0 :(得分:1)

VC2.h

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

设置委托

@interface VC2 : UIViewController <GADInterstitialDelegate>

和属性

@property(nonatomic, strong) GADInterstitial *interstitial;

VC2.m 在viewdidappear中

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

P.S。你当然也可以使用Bool for NSUserdefaults,我确实使用字符串caus我和Bools有一些错误,但Bools会更正确我猜

答案 1 :(得分:0)

对我来说,2018年的情况非常相似。

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

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

通过rewardBasedVideoAdDidClose:关闭GADOInterstitialViewController时,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是否具有显示的视图控制器并将其关闭,如果没有,则将其关闭。

在我的情况下,它非常适合:)