gpt4 book ai didi

ios - 当 AdMob 中的 GADRewardBasedVideoAd 关闭时,选项卡 Controller 也会关闭

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:00:25 25 4
gpt4 key购买 nike

我有一个带有 GADRewardBasedVideoAdViewController

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

我能做什么?

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

最佳答案

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

您可以为您的 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 中的所有 NavigationController(标题不显示并且其中没有按钮),如果我想出修复操作,我会让你知道

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com