admob插页式广告从未准备好

时间:2016-11-07 12:53:28

标签: ios swift firebase admob interstitial

我有这个游戏,我在我的gameviewcontroller中创建了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()

但它总是返回广告未准备好,而对于interstitial.isReady则返回false, 而且总是调用getInterstitial函数。

请有人帮忙吗

1 个答案:

答案 0 :(得分:3)

创建一个新的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中编写等效代码。