简体   繁体   中英

Swift(SpriteKit) / AdMob - Interstitial Ads Request Error

In AdMob, when I call interstitial ads, the log gets " Request Error: Will not send request because interstitial object has been used."

How can I fix it?

My code is:

//
//  GameViewController.swift//
//


class GameViewController: UIViewController, GADInterstitialDelegate {

    var interstitial: GADInterstitial!

    override func viewDidLoad() {
        super.viewDidLoad()

        PlayGame()

        interstitial = createAndLoadInterstitial()
        interstitial.delegate = self 

    }

    func PlayGame() {
        if let view = self.view as! SKView? {
            if let scene = MainMenu(fileNamed: "MainMenu") {
                scene.scaleMode = .aspectFit
                view.presentScene(scene)
            }

            view.ignoresSiblingOrder = false
            view.showsFPS = false
            view.showsNodeCount = false
        }
    }


    override func viewWillLayoutSubviews() {
        NotificationCenter.default.addObserver(self, selector: #selector(self.interstitialAdShow), name: NSNotification.Name(rawValue: "showGecisAd"), object: nil)
    }


    func createAndLoadInterstitial() -> GADInterstitial {
        var interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
        interstitial.delegate = self
        var request = GADRequest()
        request.testDevices = [kGADSimulatorID]
        interstitial.load(request)
        return interstitial
    }

    func interstitialDidDismissScreen(_ ad: GADInterstitial) {
        interstitial = createAndLoadInterstitial()
    }


    @objc func interstitialAdShow() {

        if interstitial.isReady {
            interstitial.present(fromRootViewController: self)
        } else {
            print("Ad wasn't ready")
        }
    }

I believe that your ad is not showing because of an ad error, so you are using the same instance again! If this is true then this will work:

func interstitial(_ ad: GADInterstitial, didFailToReceiveAdWithError error: GADRequestError {
    // you should add a delay here because if the user’s internet connection is disconnected you’ll get this called too many times too frequently
    interstitial = createAndLoadInterstitial()
}

But if you do see your ad, then your delegate method interstitialDidDismissScreen is not being called, just check why

Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM