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

查看:20
本文介绍了在 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?

我指的是这篇文章 如何使用 swift、spritekit 和 xcode 调用 admob 插页式广告? 但我想知道如何在场景之间调用广告.

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天全站免登陆