Admob Rewarded video - Root view controller issue









up vote
0
down vote

favorite












So I have two view controllers in my application:



StartViewController (Root view controller) and GameViewController



I am presenting my Rewarded video from GameViewController



Everything works perfectly, except for when the user presses "Skip now" on the rewarded video. It dismisses GameViewController and goes back to StartViewController which is my root view controller.



If the user watches the entire video, it works as intended.



The code for presenting rewarded view from GameViewController:



func playReward()

if rewardVideo!.isReady

if var topController = UIApplication.shared.keyWindow?.rootViewController

while let presentedViewController = topController.presentedViewController

// Make top controller topmost view controller
topController = presentedViewController


rewardVideo!.present(fromRootViewController: topController)





I temporarily changed the root to GameViewController to see if this was the issue and doing so fixed it, so I know it is an issue related to the root view controller and the "Skip now" button on the rewarded video.










share|improve this question























  • What do you get when you print the eventual top view controller?
    – Callam
    Nov 10 at 21:54










  • @Callam GameViewController
    – Niall Kiddle
    Nov 10 at 22:02














up vote
0
down vote

favorite












So I have two view controllers in my application:



StartViewController (Root view controller) and GameViewController



I am presenting my Rewarded video from GameViewController



Everything works perfectly, except for when the user presses "Skip now" on the rewarded video. It dismisses GameViewController and goes back to StartViewController which is my root view controller.



If the user watches the entire video, it works as intended.



The code for presenting rewarded view from GameViewController:



func playReward()

if rewardVideo!.isReady

if var topController = UIApplication.shared.keyWindow?.rootViewController

while let presentedViewController = topController.presentedViewController

// Make top controller topmost view controller
topController = presentedViewController


rewardVideo!.present(fromRootViewController: topController)





I temporarily changed the root to GameViewController to see if this was the issue and doing so fixed it, so I know it is an issue related to the root view controller and the "Skip now" button on the rewarded video.










share|improve this question























  • What do you get when you print the eventual top view controller?
    – Callam
    Nov 10 at 21:54










  • @Callam GameViewController
    – Niall Kiddle
    Nov 10 at 22:02












up vote
0
down vote

favorite









up vote
0
down vote

favorite











So I have two view controllers in my application:



StartViewController (Root view controller) and GameViewController



I am presenting my Rewarded video from GameViewController



Everything works perfectly, except for when the user presses "Skip now" on the rewarded video. It dismisses GameViewController and goes back to StartViewController which is my root view controller.



If the user watches the entire video, it works as intended.



The code for presenting rewarded view from GameViewController:



func playReward()

if rewardVideo!.isReady

if var topController = UIApplication.shared.keyWindow?.rootViewController

while let presentedViewController = topController.presentedViewController

// Make top controller topmost view controller
topController = presentedViewController


rewardVideo!.present(fromRootViewController: topController)





I temporarily changed the root to GameViewController to see if this was the issue and doing so fixed it, so I know it is an issue related to the root view controller and the "Skip now" button on the rewarded video.










share|improve this question















So I have two view controllers in my application:



StartViewController (Root view controller) and GameViewController



I am presenting my Rewarded video from GameViewController



Everything works perfectly, except for when the user presses "Skip now" on the rewarded video. It dismisses GameViewController and goes back to StartViewController which is my root view controller.



If the user watches the entire video, it works as intended.



The code for presenting rewarded view from GameViewController:



func playReward()

if rewardVideo!.isReady

if var topController = UIApplication.shared.keyWindow?.rootViewController

while let presentedViewController = topController.presentedViewController

// Make top controller topmost view controller
topController = presentedViewController


rewardVideo!.present(fromRootViewController: topController)





I temporarily changed the root to GameViewController to see if this was the issue and doing so fixed it, so I know it is an issue related to the root view controller and the "Skip now" button on the rewarded video.







ios swift admob






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 21:47









rmaddy

237k27308374




237k27308374










asked Nov 10 at 21:46









Niall Kiddle

634524




634524











  • What do you get when you print the eventual top view controller?
    – Callam
    Nov 10 at 21:54










  • @Callam GameViewController
    – Niall Kiddle
    Nov 10 at 22:02
















  • What do you get when you print the eventual top view controller?
    – Callam
    Nov 10 at 21:54










  • @Callam GameViewController
    – Niall Kiddle
    Nov 10 at 22:02















What do you get when you print the eventual top view controller?
– Callam
Nov 10 at 21:54




What do you get when you print the eventual top view controller?
– Callam
Nov 10 at 21:54












@Callam GameViewController
– Niall Kiddle
Nov 10 at 22:02




@Callam GameViewController
– Niall Kiddle
Nov 10 at 22:02












4 Answers
4






active

oldest

votes

















up vote
0
down vote













I had the same issue. I solved it by overriding the func dismiss(animated flag: Bool, completion: (() -> Void)? = nil method.



Here is what I did.



var didOpenRewardedVideo:Int = 0 
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil)
if didOpenRewardedVideo == 1
didOpenRewardedVideo = 2
super.dismiss(animated: flag, completion: completion)

else if didOpenRewardedVideo == 2
didOpenRewardedVideo = 0

else
super.dismiss(animated: flag, completion: completion)


func showRewardedVideo()

didOpenRewardedVideo = 1
GADRewardBasedVideoAd.sharedInstance().present(fromRootViewController: self)



Before showing the rewardedAd, do not forget to check if it's ready or not.



GADRewardBasedVideoAd.sharedInstance().isReady == true


When rewardedAd is presented didOpenRewardedVideo is set to 1. When user dismissing rewardedAd didOpenRewardedVideo is 1 and calling super.dismiss(animated: flag, completion: completion). Then didOpenRewardedVideo is set to 2. Now I know dismiss(animated flag: Bool, completion: (() -> Void)? = nil) will be called once more. This time I don't call the super method and set didOpenRewardedVideo to 0. I know if I dismiss my UIViewController will be dismissed.






share|improve this answer



























    up vote
    0
    down vote













    @mialkan,



    your solution does not work. I don't why no one talk about this issue. I am also facing such problem.






    share|improve this answer



























      up vote
      0
      down vote













      import GoogleMobileAds import sdk in your class or viewcontroller



      GADRewardBasedVideoAdDelegate Add this in your class or viewcontroller



      var RewardBasedVideo: GADRewardBasedVideoAd? **initialize AdController object **



      override func viewDidLoad()

      super.viewDidLoad()
      RewardBasedVideo=GADRewardBasedVideoAd.sharedInstance()
      RewardBasedVideo?.delegate = self



      //MARK:- WATCH AD BUTTON CLICK



       @IBAction func WatchAdBtn_Click(_ sender: UIButton)

      if RewardBasedVideo?.isReady == true

      RewardBasedVideo?.present(fromRootViewController: self)
      else

      //Show alert here "Ads is not ready to load"



      func rewardBasedVideoAdDidClose(_ rewardBasedVideoAd: GADRewardBasedVideoAd)

      print("Reward based video ad is closed.")






      share|improve this answer



























        up vote
        0
        down vote













        For those of you who is facing the the same issue:



        You can create a new class for your rootViewController (TabBarController or NavigationController etc.) and implement there something like that:



        override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) 
        dismissalCounter += 1
        if (dismissalCounter < 2)
        super.dismiss(animated: flag, completion: completion)



        override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)
        dismissalCounter = 0


        override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil)
        dismissalCounter = 0
        super.present(viewControllerToPresent, animated: flag, completion: completion)


        var dismissalCounter : Int = 0


        Important! Use this functions inside TabBarController or NavigationController, otherwise it is not gonna work



        UPD:
        In my case unfortunatly it breaks all NavigationControllers inside a TabBarController (titles don't show and there are no buttons inside them), if I will figure fix actions, I'll let you know



        UPD2:
        Pretty obvious decision will be to change the initialViewController and view add from it, it'll not be dismissed



        UPD3:
        I solved this very and very strange:



        class ViewController : UIViewController 
        override func viewWillAppear(_ animated: Bool)
        if GADRewardBasedVideoAd.sharedInstance().isReady == false
        let request = GADRequest()
        rewardBasedVideo!.load(request, withAdUnitID: "ca-app-pub-3940256099942544/1712485313")



        var rewardBasedVideo: GADRewardBasedVideoAd?

        @IBAction func ad_button_click(_ sender: Any)
        if rewardBasedVideo!.isReady == true
        let bl = blur()
        self.present(bl, animated: true, completion:
        self.rewardBasedVideo?.present(fromRootViewController: bl)
        )






        class blur : UIViewController
        override func viewDidLoad()
        checkForKeyWindow()


        func checkForKeyWindow()
        DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute:
        if (UIApplication.topViewController() == self)
        print("dismissed and forgotten")
        self.dismiss(animated: true, completion: nil)
        else
        print("not keywindow")
        self.checkForKeyWindow()

        )


        @objc func close()
        self.dismiss(animated: true, completion: nil)



        extension UIApplication
        class func topViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController?

        if let nav = base as? UINavigationController
        return topViewController(base: nav.visibleViewController)


        if let tab = base as? UITabBarController
        let moreNavigationController = tab.moreNavigationController

        if let top = moreNavigationController.topViewController, top.view.window != nil
        return topViewController(base: top)
        else if let selected = tab.selectedViewController
        return topViewController(base: selected)



        if let presented = base?.presentedViewController
        return topViewController(base: presented)


        return base







        share|improve this answer




















          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243729%2fadmob-rewarded-video-root-view-controller-issue%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          I had the same issue. I solved it by overriding the func dismiss(animated flag: Bool, completion: (() -> Void)? = nil method.



          Here is what I did.



          var didOpenRewardedVideo:Int = 0 
          override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil)
          if didOpenRewardedVideo == 1
          didOpenRewardedVideo = 2
          super.dismiss(animated: flag, completion: completion)

          else if didOpenRewardedVideo == 2
          didOpenRewardedVideo = 0

          else
          super.dismiss(animated: flag, completion: completion)


          func showRewardedVideo()

          didOpenRewardedVideo = 1
          GADRewardBasedVideoAd.sharedInstance().present(fromRootViewController: self)



          Before showing the rewardedAd, do not forget to check if it's ready or not.



          GADRewardBasedVideoAd.sharedInstance().isReady == true


          When rewardedAd is presented didOpenRewardedVideo is set to 1. When user dismissing rewardedAd didOpenRewardedVideo is 1 and calling super.dismiss(animated: flag, completion: completion). Then didOpenRewardedVideo is set to 2. Now I know dismiss(animated flag: Bool, completion: (() -> Void)? = nil) will be called once more. This time I don't call the super method and set didOpenRewardedVideo to 0. I know if I dismiss my UIViewController will be dismissed.






          share|improve this answer
























            up vote
            0
            down vote













            I had the same issue. I solved it by overriding the func dismiss(animated flag: Bool, completion: (() -> Void)? = nil method.



            Here is what I did.



            var didOpenRewardedVideo:Int = 0 
            override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil)
            if didOpenRewardedVideo == 1
            didOpenRewardedVideo = 2
            super.dismiss(animated: flag, completion: completion)

            else if didOpenRewardedVideo == 2
            didOpenRewardedVideo = 0

            else
            super.dismiss(animated: flag, completion: completion)


            func showRewardedVideo()

            didOpenRewardedVideo = 1
            GADRewardBasedVideoAd.sharedInstance().present(fromRootViewController: self)



            Before showing the rewardedAd, do not forget to check if it's ready or not.



            GADRewardBasedVideoAd.sharedInstance().isReady == true


            When rewardedAd is presented didOpenRewardedVideo is set to 1. When user dismissing rewardedAd didOpenRewardedVideo is 1 and calling super.dismiss(animated: flag, completion: completion). Then didOpenRewardedVideo is set to 2. Now I know dismiss(animated flag: Bool, completion: (() -> Void)? = nil) will be called once more. This time I don't call the super method and set didOpenRewardedVideo to 0. I know if I dismiss my UIViewController will be dismissed.






            share|improve this answer






















              up vote
              0
              down vote










              up vote
              0
              down vote









              I had the same issue. I solved it by overriding the func dismiss(animated flag: Bool, completion: (() -> Void)? = nil method.



              Here is what I did.



              var didOpenRewardedVideo:Int = 0 
              override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil)
              if didOpenRewardedVideo == 1
              didOpenRewardedVideo = 2
              super.dismiss(animated: flag, completion: completion)

              else if didOpenRewardedVideo == 2
              didOpenRewardedVideo = 0

              else
              super.dismiss(animated: flag, completion: completion)


              func showRewardedVideo()

              didOpenRewardedVideo = 1
              GADRewardBasedVideoAd.sharedInstance().present(fromRootViewController: self)



              Before showing the rewardedAd, do not forget to check if it's ready or not.



              GADRewardBasedVideoAd.sharedInstance().isReady == true


              When rewardedAd is presented didOpenRewardedVideo is set to 1. When user dismissing rewardedAd didOpenRewardedVideo is 1 and calling super.dismiss(animated: flag, completion: completion). Then didOpenRewardedVideo is set to 2. Now I know dismiss(animated flag: Bool, completion: (() -> Void)? = nil) will be called once more. This time I don't call the super method and set didOpenRewardedVideo to 0. I know if I dismiss my UIViewController will be dismissed.






              share|improve this answer












              I had the same issue. I solved it by overriding the func dismiss(animated flag: Bool, completion: (() -> Void)? = nil method.



              Here is what I did.



              var didOpenRewardedVideo:Int = 0 
              override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil)
              if didOpenRewardedVideo == 1
              didOpenRewardedVideo = 2
              super.dismiss(animated: flag, completion: completion)

              else if didOpenRewardedVideo == 2
              didOpenRewardedVideo = 0

              else
              super.dismiss(animated: flag, completion: completion)


              func showRewardedVideo()

              didOpenRewardedVideo = 1
              GADRewardBasedVideoAd.sharedInstance().present(fromRootViewController: self)



              Before showing the rewardedAd, do not forget to check if it's ready or not.



              GADRewardBasedVideoAd.sharedInstance().isReady == true


              When rewardedAd is presented didOpenRewardedVideo is set to 1. When user dismissing rewardedAd didOpenRewardedVideo is 1 and calling super.dismiss(animated: flag, completion: completion). Then didOpenRewardedVideo is set to 2. Now I know dismiss(animated flag: Bool, completion: (() -> Void)? = nil) will be called once more. This time I don't call the super method and set didOpenRewardedVideo to 0. I know if I dismiss my UIViewController will be dismissed.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 28 at 21:37









              mialkan

              26236




              26236






















                  up vote
                  0
                  down vote













                  @mialkan,



                  your solution does not work. I don't why no one talk about this issue. I am also facing such problem.






                  share|improve this answer
























                    up vote
                    0
                    down vote













                    @mialkan,



                    your solution does not work. I don't why no one talk about this issue. I am also facing such problem.






                    share|improve this answer






















                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      @mialkan,



                      your solution does not work. I don't why no one talk about this issue. I am also facing such problem.






                      share|improve this answer












                      @mialkan,



                      your solution does not work. I don't why no one talk about this issue. I am also facing such problem.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Dec 6 at 10:05









                      Mamunur Rahman

                      22




                      22




















                          up vote
                          0
                          down vote













                          import GoogleMobileAds import sdk in your class or viewcontroller



                          GADRewardBasedVideoAdDelegate Add this in your class or viewcontroller



                          var RewardBasedVideo: GADRewardBasedVideoAd? **initialize AdController object **



                          override func viewDidLoad()

                          super.viewDidLoad()
                          RewardBasedVideo=GADRewardBasedVideoAd.sharedInstance()
                          RewardBasedVideo?.delegate = self



                          //MARK:- WATCH AD BUTTON CLICK



                           @IBAction func WatchAdBtn_Click(_ sender: UIButton)

                          if RewardBasedVideo?.isReady == true

                          RewardBasedVideo?.present(fromRootViewController: self)
                          else

                          //Show alert here "Ads is not ready to load"



                          func rewardBasedVideoAdDidClose(_ rewardBasedVideoAd: GADRewardBasedVideoAd)

                          print("Reward based video ad is closed.")






                          share|improve this answer
























                            up vote
                            0
                            down vote













                            import GoogleMobileAds import sdk in your class or viewcontroller



                            GADRewardBasedVideoAdDelegate Add this in your class or viewcontroller



                            var RewardBasedVideo: GADRewardBasedVideoAd? **initialize AdController object **



                            override func viewDidLoad()

                            super.viewDidLoad()
                            RewardBasedVideo=GADRewardBasedVideoAd.sharedInstance()
                            RewardBasedVideo?.delegate = self



                            //MARK:- WATCH AD BUTTON CLICK



                             @IBAction func WatchAdBtn_Click(_ sender: UIButton)

                            if RewardBasedVideo?.isReady == true

                            RewardBasedVideo?.present(fromRootViewController: self)
                            else

                            //Show alert here "Ads is not ready to load"



                            func rewardBasedVideoAdDidClose(_ rewardBasedVideoAd: GADRewardBasedVideoAd)

                            print("Reward based video ad is closed.")






                            share|improve this answer






















                              up vote
                              0
                              down vote










                              up vote
                              0
                              down vote









                              import GoogleMobileAds import sdk in your class or viewcontroller



                              GADRewardBasedVideoAdDelegate Add this in your class or viewcontroller



                              var RewardBasedVideo: GADRewardBasedVideoAd? **initialize AdController object **



                              override func viewDidLoad()

                              super.viewDidLoad()
                              RewardBasedVideo=GADRewardBasedVideoAd.sharedInstance()
                              RewardBasedVideo?.delegate = self



                              //MARK:- WATCH AD BUTTON CLICK



                               @IBAction func WatchAdBtn_Click(_ sender: UIButton)

                              if RewardBasedVideo?.isReady == true

                              RewardBasedVideo?.present(fromRootViewController: self)
                              else

                              //Show alert here "Ads is not ready to load"



                              func rewardBasedVideoAdDidClose(_ rewardBasedVideoAd: GADRewardBasedVideoAd)

                              print("Reward based video ad is closed.")






                              share|improve this answer












                              import GoogleMobileAds import sdk in your class or viewcontroller



                              GADRewardBasedVideoAdDelegate Add this in your class or viewcontroller



                              var RewardBasedVideo: GADRewardBasedVideoAd? **initialize AdController object **



                              override func viewDidLoad()

                              super.viewDidLoad()
                              RewardBasedVideo=GADRewardBasedVideoAd.sharedInstance()
                              RewardBasedVideo?.delegate = self



                              //MARK:- WATCH AD BUTTON CLICK



                               @IBAction func WatchAdBtn_Click(_ sender: UIButton)

                              if RewardBasedVideo?.isReady == true

                              RewardBasedVideo?.present(fromRootViewController: self)
                              else

                              //Show alert here "Ads is not ready to load"



                              func rewardBasedVideoAdDidClose(_ rewardBasedVideoAd: GADRewardBasedVideoAd)

                              print("Reward based video ad is closed.")







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Dec 6 at 10:22









                              Jignesh Radadiya

                              6218




                              6218




















                                  up vote
                                  0
                                  down vote













                                  For those of you who is facing the the same issue:



                                  You can create a new class for your rootViewController (TabBarController or NavigationController etc.) and implement there something like that:



                                  override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) 
                                  dismissalCounter += 1
                                  if (dismissalCounter < 2)
                                  super.dismiss(animated: flag, completion: completion)



                                  override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)
                                  dismissalCounter = 0


                                  override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil)
                                  dismissalCounter = 0
                                  super.present(viewControllerToPresent, animated: flag, completion: completion)


                                  var dismissalCounter : Int = 0


                                  Important! Use this functions inside TabBarController or NavigationController, otherwise it is not gonna work



                                  UPD:
                                  In my case unfortunatly it breaks all NavigationControllers inside a TabBarController (titles don't show and there are no buttons inside them), if I will figure fix actions, I'll let you know



                                  UPD2:
                                  Pretty obvious decision will be to change the initialViewController and view add from it, it'll not be dismissed



                                  UPD3:
                                  I solved this very and very strange:



                                  class ViewController : UIViewController 
                                  override func viewWillAppear(_ animated: Bool)
                                  if GADRewardBasedVideoAd.sharedInstance().isReady == false
                                  let request = GADRequest()
                                  rewardBasedVideo!.load(request, withAdUnitID: "ca-app-pub-3940256099942544/1712485313")



                                  var rewardBasedVideo: GADRewardBasedVideoAd?

                                  @IBAction func ad_button_click(_ sender: Any)
                                  if rewardBasedVideo!.isReady == true
                                  let bl = blur()
                                  self.present(bl, animated: true, completion:
                                  self.rewardBasedVideo?.present(fromRootViewController: bl)
                                  )






                                  class blur : UIViewController
                                  override func viewDidLoad()
                                  checkForKeyWindow()


                                  func checkForKeyWindow()
                                  DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute:
                                  if (UIApplication.topViewController() == self)
                                  print("dismissed and forgotten")
                                  self.dismiss(animated: true, completion: nil)
                                  else
                                  print("not keywindow")
                                  self.checkForKeyWindow()

                                  )


                                  @objc func close()
                                  self.dismiss(animated: true, completion: nil)



                                  extension UIApplication
                                  class func topViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController?

                                  if let nav = base as? UINavigationController
                                  return topViewController(base: nav.visibleViewController)


                                  if let tab = base as? UITabBarController
                                  let moreNavigationController = tab.moreNavigationController

                                  if let top = moreNavigationController.topViewController, top.view.window != nil
                                  return topViewController(base: top)
                                  else if let selected = tab.selectedViewController
                                  return topViewController(base: selected)



                                  if let presented = base?.presentedViewController
                                  return topViewController(base: presented)


                                  return base







                                  share|improve this answer
























                                    up vote
                                    0
                                    down vote













                                    For those of you who is facing the the same issue:



                                    You can create a new class for your rootViewController (TabBarController or NavigationController etc.) and implement there something like that:



                                    override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) 
                                    dismissalCounter += 1
                                    if (dismissalCounter < 2)
                                    super.dismiss(animated: flag, completion: completion)



                                    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)
                                    dismissalCounter = 0


                                    override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil)
                                    dismissalCounter = 0
                                    super.present(viewControllerToPresent, animated: flag, completion: completion)


                                    var dismissalCounter : Int = 0


                                    Important! Use this functions inside TabBarController or NavigationController, otherwise it is not gonna work



                                    UPD:
                                    In my case unfortunatly it breaks all NavigationControllers inside a TabBarController (titles don't show and there are no buttons inside them), if I will figure fix actions, I'll let you know



                                    UPD2:
                                    Pretty obvious decision will be to change the initialViewController and view add from it, it'll not be dismissed



                                    UPD3:
                                    I solved this very and very strange:



                                    class ViewController : UIViewController 
                                    override func viewWillAppear(_ animated: Bool)
                                    if GADRewardBasedVideoAd.sharedInstance().isReady == false
                                    let request = GADRequest()
                                    rewardBasedVideo!.load(request, withAdUnitID: "ca-app-pub-3940256099942544/1712485313")



                                    var rewardBasedVideo: GADRewardBasedVideoAd?

                                    @IBAction func ad_button_click(_ sender: Any)
                                    if rewardBasedVideo!.isReady == true
                                    let bl = blur()
                                    self.present(bl, animated: true, completion:
                                    self.rewardBasedVideo?.present(fromRootViewController: bl)
                                    )






                                    class blur : UIViewController
                                    override func viewDidLoad()
                                    checkForKeyWindow()


                                    func checkForKeyWindow()
                                    DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute:
                                    if (UIApplication.topViewController() == self)
                                    print("dismissed and forgotten")
                                    self.dismiss(animated: true, completion: nil)
                                    else
                                    print("not keywindow")
                                    self.checkForKeyWindow()

                                    )


                                    @objc func close()
                                    self.dismiss(animated: true, completion: nil)



                                    extension UIApplication
                                    class func topViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController?

                                    if let nav = base as? UINavigationController
                                    return topViewController(base: nav.visibleViewController)


                                    if let tab = base as? UITabBarController
                                    let moreNavigationController = tab.moreNavigationController

                                    if let top = moreNavigationController.topViewController, top.view.window != nil
                                    return topViewController(base: top)
                                    else if let selected = tab.selectedViewController
                                    return topViewController(base: selected)



                                    if let presented = base?.presentedViewController
                                    return topViewController(base: presented)


                                    return base







                                    share|improve this answer






















                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      For those of you who is facing the the same issue:



                                      You can create a new class for your rootViewController (TabBarController or NavigationController etc.) and implement there something like that:



                                      override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) 
                                      dismissalCounter += 1
                                      if (dismissalCounter < 2)
                                      super.dismiss(animated: flag, completion: completion)



                                      override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)
                                      dismissalCounter = 0


                                      override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil)
                                      dismissalCounter = 0
                                      super.present(viewControllerToPresent, animated: flag, completion: completion)


                                      var dismissalCounter : Int = 0


                                      Important! Use this functions inside TabBarController or NavigationController, otherwise it is not gonna work



                                      UPD:
                                      In my case unfortunatly it breaks all NavigationControllers inside a TabBarController (titles don't show and there are no buttons inside them), if I will figure fix actions, I'll let you know



                                      UPD2:
                                      Pretty obvious decision will be to change the initialViewController and view add from it, it'll not be dismissed



                                      UPD3:
                                      I solved this very and very strange:



                                      class ViewController : UIViewController 
                                      override func viewWillAppear(_ animated: Bool)
                                      if GADRewardBasedVideoAd.sharedInstance().isReady == false
                                      let request = GADRequest()
                                      rewardBasedVideo!.load(request, withAdUnitID: "ca-app-pub-3940256099942544/1712485313")



                                      var rewardBasedVideo: GADRewardBasedVideoAd?

                                      @IBAction func ad_button_click(_ sender: Any)
                                      if rewardBasedVideo!.isReady == true
                                      let bl = blur()
                                      self.present(bl, animated: true, completion:
                                      self.rewardBasedVideo?.present(fromRootViewController: bl)
                                      )






                                      class blur : UIViewController
                                      override func viewDidLoad()
                                      checkForKeyWindow()


                                      func checkForKeyWindow()
                                      DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute:
                                      if (UIApplication.topViewController() == self)
                                      print("dismissed and forgotten")
                                      self.dismiss(animated: true, completion: nil)
                                      else
                                      print("not keywindow")
                                      self.checkForKeyWindow()

                                      )


                                      @objc func close()
                                      self.dismiss(animated: true, completion: nil)



                                      extension UIApplication
                                      class func topViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController?

                                      if let nav = base as? UINavigationController
                                      return topViewController(base: nav.visibleViewController)


                                      if let tab = base as? UITabBarController
                                      let moreNavigationController = tab.moreNavigationController

                                      if let top = moreNavigationController.topViewController, top.view.window != nil
                                      return topViewController(base: top)
                                      else if let selected = tab.selectedViewController
                                      return topViewController(base: selected)



                                      if let presented = base?.presentedViewController
                                      return topViewController(base: presented)


                                      return base







                                      share|improve this answer












                                      For those of you who is facing the the same issue:



                                      You can create a new class for your rootViewController (TabBarController or NavigationController etc.) and implement there something like that:



                                      override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) 
                                      dismissalCounter += 1
                                      if (dismissalCounter < 2)
                                      super.dismiss(animated: flag, completion: completion)



                                      override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)
                                      dismissalCounter = 0


                                      override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil)
                                      dismissalCounter = 0
                                      super.present(viewControllerToPresent, animated: flag, completion: completion)


                                      var dismissalCounter : Int = 0


                                      Important! Use this functions inside TabBarController or NavigationController, otherwise it is not gonna work



                                      UPD:
                                      In my case unfortunatly it breaks all NavigationControllers inside a TabBarController (titles don't show and there are no buttons inside them), if I will figure fix actions, I'll let you know



                                      UPD2:
                                      Pretty obvious decision will be to change the initialViewController and view add from it, it'll not be dismissed



                                      UPD3:
                                      I solved this very and very strange:



                                      class ViewController : UIViewController 
                                      override func viewWillAppear(_ animated: Bool)
                                      if GADRewardBasedVideoAd.sharedInstance().isReady == false
                                      let request = GADRequest()
                                      rewardBasedVideo!.load(request, withAdUnitID: "ca-app-pub-3940256099942544/1712485313")



                                      var rewardBasedVideo: GADRewardBasedVideoAd?

                                      @IBAction func ad_button_click(_ sender: Any)
                                      if rewardBasedVideo!.isReady == true
                                      let bl = blur()
                                      self.present(bl, animated: true, completion:
                                      self.rewardBasedVideo?.present(fromRootViewController: bl)
                                      )






                                      class blur : UIViewController
                                      override func viewDidLoad()
                                      checkForKeyWindow()


                                      func checkForKeyWindow()
                                      DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute:
                                      if (UIApplication.topViewController() == self)
                                      print("dismissed and forgotten")
                                      self.dismiss(animated: true, completion: nil)
                                      else
                                      print("not keywindow")
                                      self.checkForKeyWindow()

                                      )


                                      @objc func close()
                                      self.dismiss(animated: true, completion: nil)



                                      extension UIApplication
                                      class func topViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController?

                                      if let nav = base as? UINavigationController
                                      return topViewController(base: nav.visibleViewController)


                                      if let tab = base as? UITabBarController
                                      let moreNavigationController = tab.moreNavigationController

                                      if let top = moreNavigationController.topViewController, top.view.window != nil
                                      return topViewController(base: top)
                                      else if let selected = tab.selectedViewController
                                      return topViewController(base: selected)



                                      if let presented = base?.presentedViewController
                                      return topViewController(base: presented)


                                      return base








                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Dec 8 at 10:45









                                      Nikita Vasin

                                      84




                                      84



























                                          draft saved

                                          draft discarded
















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid


                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.

                                          To learn more, see our tips on writing great answers.





                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                          Please pay close attention to the following guidance:


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid


                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.

                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243729%2fadmob-rewarded-video-root-view-controller-issue%23new-answer', 'question_page');

                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Kleinkühnau

                                          Makov (Slowakei)

                                          Deutsches Schauspielhaus