OGeek|极客世界-中国程序员成长平台

标题: ios - 当 AdMob 中的 GADRewardBasedVideoAd 关闭时,标签 Controller 也会关闭 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 21:03
标题: ios - 当 AdMob 中的 GADRewardBasedVideoAd 关闭时,标签 Controller 也会关闭

我有一个带有 GADRewardBasedVideoAdViewController

我可以轻松播放和关闭广告,但随着广告的关闭 ViewController 也是如此。

我能做什么?

    @IBAction func ad_button_click(_ sender: Any) {
            if GADRewardBasedVideoAd.sharedInstance().isReady == true     
            { 
GADRewardBasedVideoAd.sharedInstance().present(fromRootViewController: self)
            }
        }



Best Answer-推荐答案


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

您可以为您的 rootViewController(TabBarControllerNavigationController 等)创建一个新类并在那里实现类似的东西:

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 中使用此函数,否则将不起作用

统一更新: 在我的情况下,不幸的是它破坏了 TabBarController 中的所有 NavigationControllers(标题不显示,并且其中没有按钮),如果我想修复操作,我会告诉你

UPD2: 很明显的决定是更改 initialViewController 并从中添加 View ,它不会被解雇

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

关于ios - 当 AdMob 中的 GADRewardBasedVideoAd 关闭时,标签 Controller 也会关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53674828/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4