3

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.

EDIT 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() {
    super.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 {

        showInterstitial()
        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 = ["..."]
    ad.loadRequest(request)
    return ad
}

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






override func didReceiveMemoryWarning() {
    super.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 true to the "adToBeShown" bool. This is not the best method, so could anyone tell me how to directly call the ad when a scene is presented?

Community
  • 1
  • 1
Dieblitzen
  • 544
  • 4
  • 22
  • What have you tried? I would present an ad before transitioning to the next scene. In the interstitial delegate method that's called when the ad is dismissed by the user I'd put my code to transition to the next scene. I'd also put the same code in the delegate method that is called when an ad fails to load. If you only want to show ads say every other time, set up an index that you increment and an `if` statement to decide whether you show an ad or not based on the value of the index, ie. `if index % 2 == 0`. – Daniel Storm Dec 24 '15 at 15:13
  • The problem is I configured the interstitial ads in the GameViewController, and I don't know how to call the ads in a scene. Nor do I know what code to use in the GameViewController to check which scene is currently being displayed. If you know how to do this, could you post a solution? Thanks a ton. @Daniel – Dieblitzen Dec 24 '15 at 18:06
  • Ok I do not know if this is a good method, and I believe it's very inefficient, but I used `NSTimer.scheduledTimerWithTimeInterval(...)`. I created an `NSUserDefaults.standardUserDefaults()` bool value, and assigned it `true` when my GameOverScene was presented. As you can guess, I used the timer in the GameViewController to constantly check for the bool and present the ad when it was `true`. Is this method good or should I use something better? Thanks. @Daniel – Dieblitzen Dec 25 '15 at 03:53
  • Edit your question to include your AdMob implementation. – Daniel Storm Dec 25 '15 at 03:55
  • I'm showing you what I've done so far, it works, but I think there are neater methods? Thanks @Daniel – Dieblitzen Dec 25 '15 at 04:03

2 Answers2

5

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

crashoverride777
  • 10,581
  • 2
  • 32
  • 56
  • Where in GameViewController should I add this? Thanks @crashoverride – Dieblitzen Dec 26 '15 at 09:56
  • It worked! Thank you so much! Could you also show the delegate method? But thanks a ton! @crashoverride – Dieblitzen Dec 26 '15 at 10:05
  • to use delegates you should read this, i think it explains it well. Let me know how it goes. https://b4sht4.wordpress.com/2014/12/02/implementing-a-delegate-pattern-between-spritekit-scenes/ – crashoverride777 Dec 26 '15 at 21:43
  • Now that iAds are shutting down, does your github project still work ( I mean it will work but will it be scrutinized when the app is submitted for review?) – C. Wagner Feb 25 '16 at 05:20
  • IAds itself are not shutting down, it's the iAd app network that's shutting down. Should be business as usual so far, not really sure what will happen in June when the automated system or whatever is taking over. – crashoverride777 Feb 26 '16 at 02:10
  • Actually according to some new information it might actually be all going away. I am not sure what's going on exactly and if new apps get reject. You can read more here https://www.reddit.com/r/iOSProgramming/comments/45e22e/discontinuation_of_iad/ – crashoverride777 Feb 26 '16 at 02:54
0

You can also limit the frequency that Admob will show interstitial ads via the Admob website. In the settings for your interstitial advert, there are options to change the frequency of adverts popping up.

Iain Coleman
  • 166
  • 1
  • 13