ios - 当 AdMob 中的 GADRewardBasedVideoAd 关闭时,标签 Controller 也会关闭
<p><p>我有一个带有 <code>GADRewardBasedVideoAd</code> 的 <code>ViewController</code>。</p>
<p>我可以轻松播放和关闭广告,但随着广告的关闭 <code>ViewController</code> 也是如此。</p>
<p>我能做什么?</p>
<pre><code> @IBAction func ad_button_click(_ sender: Any) {
if GADRewardBasedVideoAd.sharedInstance().isReady == true
{
GADRewardBasedVideoAd.sharedInstance().present(fromRootViewController: self)
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>对于那些面临同样问题的人:</p>
<p>您可以为您的 <code>rootViewController</code>(<code>TabBarController</code> 或 <code>NavigationController</code> 等)创建一个新类并在那里实现类似的东西:</p >
<pre><code>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
</code></pre>
<p>重要!在 <code>TabBarController</code> 或 <code>NavigationController</code> 中使用此函数,否则将不起作用</p>
<p><strong>统一更新:</strong>
在我的情况下,不幸的是它破坏了 <code>TabBarController</code> 中的所有 NavigationControllers(标题不显示,并且其中没有按钮),如果我想修复操作,我会告诉你</p>
<p><strong>UPD2:</strong>
很明显的决定是更改 <code>initialViewController</code> 并从中添加 View ,它不会被解雇</p>
<p><strong>UPD3:</strong>
我解决了这个非常非常奇怪的问题:</p>
<pre><code>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
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 当 AdMob 中的 GADRewardBasedVideoAd 关闭时,标签 Controller 也会关闭,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/53674828/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/53674828/
</a>
</p>
页:
[1]