简体   繁体   中英

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.

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM