Dieblitzen Dieblitzen - 1 year ago 144
iOS Question

Show Admob Interstitial ads between scenes in Swift SpriteKit

I would like to know how to set up Admob Interstitial ads when I present my GameOverScene. What should I do to show the ads only sometimes when the game gets over? And how do I implement this in Swift?

Im referring to this post How to call admob interstitial ad using swift, spritekit and xcode? but i'd like to know how to call the ads in between scenes.

Here is the code I used to present the ad

class GameViewController: UIViewController, GADInterstitialDelegate {

var interstitial = GADInterstitial()
var intersitialRecievedAd = false
let defaults = NSUserDefaults.standardUserDefaults()

override func viewDidLoad() {

interstitial.delegate = self
self.interstitial = createAndLoadAd()

let timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "checkIfAdIsToBeDisplayed:", userInfo: nil, repeats: true)

//Scene implementation, boring stuff, got nothing to do with the ads...

func checkIfAdIsToBeDisplayed(timer:NSTimer) {

if defaults.boolForKey("adToBeShown") == true && intersitialRecievedAd == true {

defaults.setBool(false, forKey: "adToBeShown")
intersitialRecievedAd = false

} else {



func interstitialDidReceiveAd(ad: GADInterstitial!) {

intersitialRecievedAd = true


func createAndLoadAd() -> GADInterstitial {
var ad = GADInterstitial(adUnitID: "...")
ad.delegate = self
let request = GADRequest()
request.testDevices = ["..."]
return ad

func showInterstitial(){
if self.interstitial.isReady {
self.interstitial = createAndLoadAd()

override func didReceiveMemoryWarning() {
// Release any cached data, images, etc that aren't in use.

override func prefersStatusBarHidden() -> Bool {
return true

This code uses a timer to constantly check whether the gameOverScene has been presented. When the gameOverScene IS presented, I assign
to the
This is not the best method, so could anyone tell me how to directly call the ad when a scene is presented?

Answer Source

why dont you use NSNotificationCenter or delegates to call showInterstitial().

For example

In gameViewController add this in viewDidLoad

 NSNotificationCenter.defaultCenter().addObserver(self, selector: "showInterstitial"), name:"showInterAdKey", object: nil);

and when you want to show the ad from your scenes you use

 NSNotificationCenter.defaultCenter().postNotificationName("showInterAdKey", object: nil)

You could also use something like the helper I posted on github which makes this even easier and your ViewController stays clean https://github.com/crashoverride777/Swift-2-iAds-and-AdMob-Helper

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download