Admob Rewarded视频-根视图控制器问题



所以我的应用程序中有两个视图控制器:

StartViewController (Root view controller)GameViewController

我正在展示GameViewController的奖励视频

除了用户在奖励视频上按下"立即跳过"外,一切都很完美。它解除GameViewController并返回到StartViewController,它是我的根视图控制器。

如果用户观看了整个视频,它将按预期工作。

GameViewController:显示奖励视图的代码

func playReward()
{
if rewardVideo!.isReady
{
if var topController = UIApplication.shared.keyWindow?.rootViewController
{
while let presentedViewController = topController.presentedViewController
{
// Make top controller topmost view controller
topController = presentedViewController
}
rewardVideo!.present(fromRootViewController: topController)
}
}
}

我暂时将root更改为GameViewController,看看这是否是问题所在,并修复了它,所以我知道这是一个与root视图控制器和奖励视频上的"立即跳过"按钮有关的问题。

我遇到了同样的问题。我通过重写func dismiss(animated flag: Bool, completion: (() -> Void)? = nil方法解决了这个问题。

以下是我所做的。

var didOpenRewardedVideo:Int = 0 
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
if didOpenRewardedVideo == 1 {
didOpenRewardedVideo = 2
super.dismiss(animated: flag, completion: completion)
}
else if didOpenRewardedVideo == 2{
didOpenRewardedVideo = 0
}
else{
super.dismiss(animated: flag, completion: completion)
}
}
func showRewardedVideo()
{
didOpenRewardedVideo = 1
GADRewardBasedVideoAd.sharedInstance().present(fromRootViewController: self)
}

在展示重新编织的iPad之前,不要忘记检查它是否准备好。

GADRewardBasedVideoAd.sharedInstance().isReady == true

当呈现再加电压d时,CCD_ 9被设置为1。当用户解除转发时,则CCD_ 10为1,并调用CCD_。则CCD_ 12被设置为2。现在我知道dismiss(animated flag: Bool, completion: (() -> Void)? = nil)将再次被调用。这次我不调用super方法,而是将didOpenRewardedVideo设置为0。我知道如果我解雇我的UIViewController就会被解雇。

@mialkan,

您的解决方案不起作用。我不明白为什么没有人谈论这个问题。我也面临着这样的问题。

导入GoogleMobileAds导入类或视图控制器中的sdk

GADRewardBasedVideoAdDelegate将其添加到您的类或视图控制器

var RewardBasedVideo:GADRewardBasedVideoAd**初始化AdController对象**

override func viewDidLoad()
{
super.viewDidLoad()
RewardBasedVideo=GADRewardBasedVideoAd.sharedInstance()
RewardBasedVideo?.delegate = self
}

//标记:-观看广告按钮点击

@IBAction func WatchAdBtn_Click(_ sender: UIButton)
{
if RewardBasedVideo?.isReady == true
{
RewardBasedVideo?.present(fromRootViewController: self)
} else
{
//Show alert here "Ads is not ready to load"
}
}
func rewardBasedVideoAdDidClose(_ rewardBasedVideoAd: GADRewardBasedVideoAd) 
{
print("Reward based video ad is closed.")
}

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

您可以为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中使用此功能,否则它将无法在中工作

UPD:在我的情况下,不幸的是,它破坏了TabBarController中的所有导航控制器(标题不显示,里面也没有按钮(,如果我想修复操作,我会让你知道

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

最新更新