Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios GadPresentFromRootViewController导致当前视图控制器关闭_Ios_Uiviewcontroller_Admob_Interstitial - Fatal编程技术网

Ios GadPresentFromRootViewController导致当前视图控制器关闭

Ios GadPresentFromRootViewController导致当前视图控制器关闭,ios,uiviewcontroller,admob,interstitial,Ios,Uiviewcontroller,Admob,Interstitial,当我的应用程序的用户单击某个按钮转到某个ViewController时,我试图显示一个中间广告。在新的ViewController的viewDidLoad方法中,我检查Gadital ad是否已准备就绪,如果已准备就绪,我将尝试显示它。问题是,当我关闭该广告时,我注意到显示该广告的ViewController已不存在,而是将我发送回启动显示该广告的ViewController的ViewController 这是我的密码: - (void)viewDidLoad { [super viewD

当我的应用程序的用户单击某个按钮转到某个ViewController时,我试图显示一个中间广告。在新的ViewController的viewDidLoad方法中,我检查Gadital ad是否已准备就绪,如果已准备就绪,我将尝试显示它。问题是,当我关闭该广告时,我注意到显示该广告的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;
}
 }
  ViewController 1
   -> ViewController 2 (via presentViewController:animated:completion)
    -> GADOInterstitialViewController (presentFromRootViewController:)   
 - (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion {

    if(self.presentedViewController != nil){
        [self.presentedViewController dismissViewControllerAnimated:flag completion:completion];
    } else {
        [super dismissViewControllerAnimated:flag completion:completion];
    }
}
我确实注意到,当我展示广告时,日志中会显示以下信息:

  Presenting view controllers on detached view controllers is discouraged BTRIndividualLocationViewController: 0x7fd63bcd9c00.
如果我尝试在ViewWillEmbeen中显示它,同样的问题也会出现

我做错了什么

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

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

    [helper.interstitial presentFromRootViewController:rootController];
但结果仍然相同,只是日志中没有弹出消息。

VC2.h

#import "GADInterstitial.h"
#import "GADInterstitialDelegate.h"
集合委托

@interface VC2 : UIViewController <GADInterstitialDelegate>
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中间隙的所有代码包装在ViewDid中,并显示在:

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

}
并添加到-(void)interstituraldireceivead:(gadistitural*)ad{


p、 当然,我确实使用了字符串,因为我在Bools上有一些错误,但是Bools会更正确,我想2018年对我来说是一个非常类似的场景

我当前的ViewController如下所示:

    -(BOOL)isInterstitialReady {
if ([self.interstitial isReady]) {
    return YES;
} else {
    return NO;
}
 }
  ViewController 1
   -> ViewController 2 (via presentViewController:animated:completion)
    -> GADOInterstitialViewController (presentFromRootViewController:)   
 - (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion {

    if(self.presentedViewController != nil){
        [self.presentedViewController dismissViewControllerAnimated:flag completion:completion];
    } else {
        [super dismissViewControllerAnimated:flag completion:completion];
    }
}
当通过
RewardBasedVideoAddicClose:
解除GADOINTERSTIALVIEWCONTROLLER时,ViewController 2也会立即解除

我已经找到了这个问题的一个简单解决方案。只需覆盖ViewController 2函数
dismissViewControllerAnimated
,如下所示:

    -(BOOL)isInterstitialReady {
if ([self.interstitial isReady]) {
    return YES;
} else {
    return NO;
}
 }
  ViewController 1
   -> ViewController 2 (via presentViewController:animated:completion)
    -> GADOInterstitialViewController (presentFromRootViewController:)   
 - (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将被解除


这在我的例子中非常有效:)

可能只是在VC中执行所有你想实际显示的填隙内容。使用NSUserdefaults,当用户按下按钮将值设置为NSUserdefaults时,在VC中显示该NSUserdefaults值的填隙检查,如果显示填隙以及何时触发重置下一次NSUserdefaults的值…!?我的意思是,你甚至不需要NSUserdefaults的东西,为什么你要在另一个不想显示的VC中启动Interstitual?你想在每次显示VC时都显示Interstitual?或者仅当它来自一个特殊的按钮时?如果是这样,使用NSUserdefaults,但Interstitual st你想在VC中显示uff,而不是在另一个VC中。我想在一个VC(vc1)中,它上面有一个按钮,可以启动另一个VC(vc2).基本上,在从vc1到vc2的过渡过程中,我希望广告弹出。当你关闭广告时,vc2应该在那里等待。你上面看到的所有代码都在vc2中。是的,vc2中的所有中间内容也是如此。你为什么在vc1中这样做?我想我的问题一定不清楚,因为我认为你不理解它。我的问题不是w仅在某些情况下显示广告。我的问题是,当我退出广告时,为什么我的视图控制器会关闭?哦,好吧,我想这与你的helper类有关。为什么使用helper类?你会将其用于共享横幅单例,但对于间隙则没有必要!?只需使用我的代码,它只有几行,并且可以工作…不需要ed for the Helper and do it in ViewDidDisplay,viewdidload只加载一次,并非每次都显示视图。事实上,我只是想出来了。这是我在自定义UIPageCotronner中进行的一些其他有趣的事情导致了此关闭。感谢您的努力和提示。我仍然会给您公认的答案。是的,我有一开始我以为我需要它的原因,但现在我都不记得那是什么了。我可能会把它扔掉。