当AdMob中的GADRewardBasedVideoAd关闭时,Tab Controller也将关闭

时间:2018-12-07 18:10:04

标签: ios swift admob

我有一个df$output <- rep(NA, nrow(df)) for(i in 1:nrow(df)){ if(!all(df[i, 6:10] == "")){ df$output[i] <- paste0(paste0(df[i, 1:5], collapse = ""), "-", paste0(df[i, 6:10], collapse = "")) } else { df$output[i] <- paste0(df[i, 1:5], collapse = "") } } 和一个ViewController

我可以轻松播放和关闭广告,但同时也关闭GADRewardBasedVideoAd

我该怎么办?

ViewController

1 个答案:

答案 0 :(得分:0)

对于那些面临相同问题的人:

您可以为rootViewControllerTabBarControllerNavigationController等)创建一个新类,并在其中实现类似的内容:

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

重要!在TabBarControllerNavigationController中使用此功能,否则将无法正常工作

UPD: 对于我而言,很不幸,它会破坏TabBarController内的所有NavigationController(标题没有显示,并且里面没有按钮),如果我想解决问题的话,我会告诉你

UPD2: 显而易见的决定是更改initialViewController并从中查看添加内容,不会被删除

UPD3: 我解决了这个非常非常奇怪的问题:

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
    }
}