【问题标题】:admob interstitial ad is never ready [duplicate]admob 插页式广告从未准备好[重复]
【发布时间】:2016-11-07 12:53:28
【问题描述】:

我有这个游戏,我在我的游戏视图控制器中创建了 3 个函数,它们就在这里

    func getInterstitialAd(){
    interstitial = GADInterstitial(adUnitID: "ca-app-pub-1782852253088296/5018877964")
    let requestInterstitial = GADRequest()
    interstitial.load(requestInterstitial)
}



func showAd() {

    if (interstitial.isReady == true){
        interstitial.present(fromRootViewController: GameViewController())
    }else{
        print("ad wasn't ready")
        interstitial = createAd()
    }



}

func createAd() -> GADInterstitial{
    let interstital = GADInterstitial(adUnitID: "ca-app-pub-1782852253088296/5018877964")
    interstitial.load(GADRequest())
    return interstital
}

在我的一个名为 StartMenu 的场景中,我调用了这些函数

   var viewController: GameViewController!

然后我调用函数

       viewController.getInterstitialAd()
        viewController.showAd()

但它总是返回 ad not ready ,并且对于 interstitial.isReady 为 false, 而且总是调用 getInterstitial 函数。

有人可以帮忙吗

【问题讨论】:

  • @DanielStorm 我确定没有,我真的不知道如何解决这个问题,请帮助

标签: ios swift firebase admob interstitial


【解决方案1】:

创建一个新的 swift 文件 AdMobDelegate :-

import UIKit
import GoogleMobileAds

class AdMobDelegate: NSObject, GADInterstitialDelegate {

    var interstitialView: GADInterstitial!

    func createAd() -> GADInterstitial {
        interstitialView = GADInterstitial(adUnitID: "Your Key")
        interstitialView.delegate = self
        let request = GADRequest()
        interstitialView.loadRequest(request)
        return interstitialView
    }

    func showAd() {
        if interstitialView != nil {
            if (interstitialView.isReady == true){
                interstitialView.present(fromRootViewController:currentVc)
            } else {
                print("ad wasn't ready")
                interstitialView = createAd()
            }
        } else {
            print("ad wasn't ready")
            interstitialView = createAd()
        }
    }

    func interstitialDidReceiveAd(ad: GADInterstitial!) {
        print("Ad Received")
        if ad.isReady {
            interstitialView.present(fromRootViewController: currentVc)
        }
   }

    func interstitialDidDismissScreen(ad: GADInterstitial!) {
        print("Did Dismiss Screen")
    }

    func interstitialWillDismissScreen(ad: GADInterstitial!) {
        print("Will Dismiss Screen")
    }

    func interstitialWillPresentScreen(ad: GADInterstitial!) {
        print("Will present screen")
    }

    func interstitialWillLeaveApplication(ad: GADInterstitial!) {
        print("Will leave application")
    }

    func interstitialDidFailToPresentScreen(ad: GADInterstitial!) {
        print("Failed to present screen")
    }

    func interstitial(ad: GADInterstitial!, didFailToReceiveAdWithError error: GADRequestError!) {
        print("\(ad) did fail to receive ad with error \(error)")
    }
}

现在您可以在其他文件中使用此委托类的对象,如下所示:-

//Define admobdelegate as global variable
var admobDelegate = AdMobDelegate()

//Declare a global variable currentVc to hold reference to current view controller
var currentVc: UIViewController!

class abc1: UIViewController {

    override func viewdidload() {
        super.viewdidload()
        currentVc = self
        admobDelegate.showAd()
    }

    override func viewDidAppear() {
        super.viewDidAppear()
        currentVc = self
    }
}

class abc2: UIViewController {

    override func viewdidload() {
        super.viewdidload()
        currentVc = self
        admobDelegate.showAd()
    }

    override func viewDidAppear() {
        super.viewDidAppear()
        currentVc = self
    }
}

代码在 Swift 2.2 中。在出现语法错误的情况下,在 swift 3 中编写等效代码。

【讨论】:

  • 非常感谢,终于开始工作了!!!几天来我一直在绞尽脑汁想弄清楚。
  • 还有一个问题,如果我想从另一个函数(如游戏场景)调用,我能做到的最好方法是什么?
  • 只需在该函数中调用 showAd() 即可。应该自己照顾
  • 在我的游戏场景中,我创建了一个对象 var viewController: GameViewController!然后像 viewController.showAd() 那样调用它,但游戏崩溃了
  • gamescene是同一个类还是不同类的函数?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多