Swift(SpriteKit)/ AdMob - 非页内广告请求错误

时间:2018-01-30 18:18:09

标签: ios swift xcode admob interstitial

在AdMob中,当我调用插页式广告时,日志会显示“请求错误:因为已使用插页式广告对象而无法发送请求。”

我该如何解决?

我的代码是:

//
//  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 个答案:

答案 0 :(得分:0)

我认为您的广告因广告错误而未展示,因此您再次使用相同的实例!如果这是真的那么这将起作用:

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()
}

但是,如果您确实看到了自己的广告,那么您的代理方法interstitialDidDismissScreen未被调用,只需检查原因

祝你好运