所以我的应用程序中有两个视图控制器:
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)
}
}
}
我暂时将根更改为GameViewController
,以查看是否是问题所在,并进行了修复,因此我知道这是与根视图控制器和奖励上的“立即跳过”按钮有关的问题视频。
答案 0 :(得分:0)
我有同样的问题。我通过重写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)
}
在显示奖励广告之前,请不要忘记检查广告是否已准备好。
GADRewardBasedVideoAd.sharedInstance().isReady == true
当出现rewardedAd时,didOpenRewardedVideo
设置为1。当用户关闭rewardedAd时,didOpenRewardedVideo
为1,然后调用super.dismiss(animated: flag, completion: completion)
。然后将didOpenRewardedVideo
设置为2。现在我知道dismiss(animated flag: Bool, completion: (() -> Void)? = nil)
将再次被调用。这次我不调用super
方法并将didOpenRewardedVideo
设置为0。我知道如果我解雇了UIViewController
,我将被解雇。
答案 1 :(得分:0)
@mialkan,
您的解决方案不起作用。我不为什么没有人谈论这个问题。我也面临这样的问题。
答案 2 :(得分:0)
导入GoogleMobileAds 导入类或ViewController中的SDK
GADRewardBasedVideoAdDelegate 将此添加到您的类或ViewController中
var RewardBasedVideo:GADRewardBasedVideoAd? **初始化AdController对象**
override func viewDidLoad()
{
super.viewDidLoad()
RewardBasedVideo=GADRewardBasedVideoAd.sharedInstance()
RewardBasedVideo?.delegate = self
}
// MARK:-观看广告按钮点击
@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.")
}
答案 3 :(得分:0)
对于那些面临相同问题的人:
您可以为rootViewController
(TabBarController
或NavigationController
等)创建一个新类,并在其中实现类似的内容:
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
重要!在TabBarController
或NavigationController
中使用此功能,否则将无法正常工作
UPD:
对于我而言,很不幸,它会破坏TabBarController
内的所有NavigationController(标题没有显示,并且里面没有按钮),如果我想解决问题的话,我会告诉你
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
}
}