在Swift SpriteKit中的场景之间显示Admob非页内广告 [英] Show Admob Interstitial ads between scenes in Swift SpriteKit

查看:95
本文介绍了在Swift SpriteKit中的场景之间显示Admob非页内广告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我展示GameOverScene时,我想知道如何设置Admob非页内广告。我应该怎么做才能仅在游戏结束后才显示广告?以及我该如何在Swift中实现呢?

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

此代码使用一个计时器,用于不断检查是否显示了gameOverScene。当显示gameOverScene时,我将 true 分配给 adToBeShown 布尔值。
这不是最好的方法,所以有人可以告诉我如何在呈现场景时直接调用广告吗?

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?

推荐答案

为什么不使用NSNotificationCenter或委托调用showInterstitial()。

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

例如

在GameViewController中将其添加到viewDidLoad

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)

您还可以使用类似我在github上发布的帮助器的方法,这使此操作变得更加容易,并且ViewController保持干净 https://github.com/crashoverride777/Swift-2-iAds-and-AdMob-Helper

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

这篇关于在Swift SpriteKit中的场景之间显示Admob非页内广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆