• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 当 AdMob 中的 GADRewardBasedVideoAd 关闭时,标签 Controller 也会关闭

[复制链接]
菜鸟教程小白 发表于 2022-12-11 21:03:33 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个带有 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap