【问题标题】:Show Admob Interstitial ads between scenes in Swift SpriteKit在 Swift SpriteKit 中的场景之间显示 Admob 插页式广告
【发布时间】:2015-12-24 11:41:48
【问题描述】:

我想知道在展示我的 GameOverScene 时如何设置 Admob 插页式广告。我应该怎么做才能只在游戏结束时才显示广告?以及如何在 Swift 中实现这一点?

我指的是这篇文章How to call admob interstitial ad using swift, spritekit and xcode?,但我想知道如何在场景之间调用广告。

编辑 这是我用来展示广告的代码

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" 布尔值。 这不是最好的方法,谁能告诉我如何在呈现场景时直接调用广告?

【问题讨论】:

  • 你试过什么?在过渡到下一个场景之前,我会展示一个广告。在广告被用户关闭时调用的插页式委托​​方法中,我会将代码转换到下一个场景。我还将相同的代码放在广告加载失败时调用的委托方法中。如果您只想每隔一段时间展示一次广告,请设置一个您递增的索引和一个if 语句,以根据索引的值来决定是否展示广告,即。 if index % 2 == 0.
  • 问题是我在GameViewController中配置了插屏广告,不知道如何调用场景中的广告。我也不知道在 GameViewController 中使用什么代码来检查当前正在显示哪个场景。如果你知道如何做到这一点,你能发布一个解决方案吗?万分感谢。 @丹尼尔
  • 好吧我不知道这是不是一个好方法,而且我相信它效率很低,但是我使用了NSTimer.scheduledTimerWithTimeInterval(...)。我创建了一个 NSUserDefaults.standardUserDefaults() 布尔值,并在我的 GameOverScene 出现时为其分配了 true。你可以猜到,我使用 GameViewController 中的计时器不断检查布尔值并在广告为 true 时显示广告。这种方法好还是我应该使用更好的方法?谢谢。 @丹尼尔
  • 修改您的问题以包含您的 AdMob 实施。
  • 我正在向您展示我到目前为止所做的事情,它有效,但我认为还有更简洁的方法?谢谢@丹尼尔

标签: ios sprite-kit admob swift2 interstitial


【解决方案1】:

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

例如

在 gameViewController 中添加 viewDidLoad

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

当你想从你使用的场景中展示广告时

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

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

【讨论】:

  • 我应该在 GameViewController 的哪个位置添加这个?谢谢@crashoverride
  • 成功了!太感谢了!您还可以显示委托方法吗?但是非常感谢! @crashoverride
  • 使用代表你应该阅读这个,我认为它解释得很好。让我知道事情的后续。 b4sht4.wordpress.com/2014/12/02/…
  • 现在 iAds 正在关闭,您的 github 项目是否仍然有效(我的意思是它会有效,但在提交应用以供审核时会对其进行审查吗?)
  • IAds 本身并没有关闭,而是 iAd 应用网络正在关闭。到目前为止应该一切照旧,不确定 6 月份自动化系统或其他什么会接管时会发生什么。
【解决方案2】:

您还可以限制 Admob 通过 Admob 网站展示插页式广告的频率。在插页式广告的设置中,有一些选项可以更改广告弹出的频率。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多