0
votes

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")
        }
    }
1
Your code looks ok, can you show the code that uses that class? (Apparently it’s your ViewController) - Yitzchak
İn my GameScene if player died, i using this code : NotificationCenter.default.post(name: NSNotification.Name(rawValue: "showGecisAd"), object: nil) - Erdem KARAKAYA

1 Answers

0
votes

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