Steff C Steff C - 4 months ago 16
Swift Question

interstitialWillDismissScreen only being called once

I am using Admob interstitials in my swift project and I am having trouble reloading the ads. The first interstitial displays fine, and when interstitialWillDismissScreen is called, it exits back to the game and reloads a new interstitial as it's supposed to. However, neither interstitialDidReceiveAd nor interstitialWillDismissScreen are called again, so after the second ad is displayed, no other ads come through. What am I missing?

import GoogleMobileAds

let appDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)

class GameScene: SKScene, GKGameCenterControllerDelegate, GADInterstitialDelegate {

var interstitial: GADInterstitial!

override func didMoveToView(view: SKView) {

//preload interstitial ad
if NSUserDefaults.standardUserDefaults().boolForKey("paidToRemoveAds") == false {
interstitial = loadAd()
}

interstitial.delegate = self
}

func gameOver() {
runGoogleAd()
}

func loadAd() -> GADInterstitial {
let ad = GADInterstitial(adUnitID: "ca-app-pub-2963481692578498/7292484569")
let request = GADRequest()
request.testDevices = [kGADSimulatorID]
ad.loadRequest(request)
return ad
}

func runGoogleAd() {
if interstitial.isReady {
interstitial.presentFromRootViewController((appDelegate.window?.rootViewController)!)
}
}

func interstitialDidReceiveAd(ad: GADInterstitial!) {
print("ad loaded")
}

func interstitialWillDismissScreen(ad: GADInterstitial!) {
interstitial = loadAd()
print("loading new ad")
}
}

Answer

I assume loadAd() creates a new instance? You need to set the delegate on the new instance:

func interstitialWillDismissScreen(ad: GADInterstitial!) {
    interstitial = loadAd()

    interstitial.delegate = self  // <-- You forgot this.

    print("loading new ad")
}

Actually you should probably set the delegate in loadAd() since it is an instance member of this class.

Comments