【问题标题】:Swift Admob Interstitial ErrorSwift Admob 插页式错误
【发布时间】:2016-05-26 08:19:54
【问题描述】:

这很长,但它可能很容易解决。 大部分是代码,直接跳到底部就会有一个tl;dr

嘿,我已经学习了大约 10 个关于让 Admob 插页式广告在我的 Swift Spritekit 游戏中工作的教程。除了我见过的所有教程都使用带有按钮的 IBAction。 目前我希望广告在菜单上每隔一段时间弹出一次。 (刚开始,我以后可能会改变它)。 这一切都很完美,但是一旦我按下播放按钮(进入不同的场景)然后返回菜单场景。 (GameScene) 插页式功能不再发生。没有错误,只是在应用程序完全关闭并重新打开之前不再执行此操作。

这是代码。

在我的 GameViewController 类中,我有:

var interstital: GADInterstitial!

在 GameViewController 中确实加载了我:

if let scene = GameScene(fileNamed:"GameScene") {
        let skView = self.view as? SKView
        skView!.ignoresSiblingOrder = false
        scene.scaleMode = .AspectFill

        scene.viewController = self
        skView!.presentScene(scene)

        interstital = GADInterstitial(adUnitID: "ca-app-pub-9776687746813194/9687097060")

        let request = GADRequest()
        interstital.loadRequest(request)

在 GameViewController 类中我也有这些功能:

func ShowAd() {
    print("doing the showadfunc now")

    if (interstital.isReady) {

        print("there is an inter ready")

        interstital.presentFromRootViewController(self)
        interstital = CreateAd()

    }
    else{
        print("The interstitial wasn't ready.")
    }
}

func CreateAd() -> GADInterstitial {

    let interstital = GADInterstitial(adUnitID: "ca-app-pub-9776687748683194/9687247060")
    interstital.loadRequest(GADRequest())
    return interstital

}

在我的 GameScene 的(菜单)类中:

var viewController: GameViewController!

在我的 GameScene 课程中,我有这个用于显示插页式广告的功能。

func adThing(){
    //viewController?.ShowAd()

    print("starting the ad function")
    let waitForInterstitial = SKAction.waitForDuration(15.0)
    let waitForInterstitialShort = SKAction.waitForDuration(3.0)
    let showInterstitialAd = SKAction.runBlock({self.viewController?.ShowAd(); print("the function has been called..")})
    let waitThenShowInterstitial = SKAction.sequence([showInterstitialAd,waitForInterstitial])
    let waitThenShowInterStitialForever = SKAction.repeatActionForever(waitThenShowInterstitial)
    //self.runAction(waitThenShowInterStitialForever)
    self.runAction(waitForInterstitialShort, completion: {
        print("its waited 3 seconds and will now start the ad thingo")
        self.runAction(waitThenShowInterStitialForever)
    })
    print("done")

}

所以,我每 20 秒在我的 GameScene 中调用 ShowAd 函数(位于 gameviewcontroller 中),(我已经确认它至少认为它正在调用该函数。)该应用程序首先打开。但是当我更改场景然后返回菜单场景 (GameScene) 时,我的 GameScene 函数中的打印不断发生,我的代码 认为 它正在调用 ShowAd 函数,但似乎什么都没有发生这种情况时,不会弹出任何插页式广告,甚至 GameViewController 中 ShowAd 函数中的打印也不会弹出。

所以在我改变场景并返回之后,我的游戏似乎忘记了 GameViewController 是什么。如果我指的是没有“?”的 GameViewController它会使我的游戏崩溃,并显示错误“在展开可选选项时意外发现 nil”。那么换个场景之后,也许 GameViewController 就变成了 nil 了?我该如何解决这个问题。

请帮帮我!!! (如果你不知道,我是编程新手。我知道对我的代码发表讽刺评论一定很诱人,它真的很简单,目前还不是很好。)

如果你能帮助我,非常感谢,哇.. 感谢您阅读所有这些.. ;)

【问题讨论】:

    标签: ios swift null admob interstitial


    【解决方案1】:

    您的问题很可能是,一旦您转换到新场景,viewController 属性会设置为 nil,因此如果您使用 !,则会崩溃。

    一般来说,在您的 SKScene 中引用 viewController 不是最佳做法,您应该使用委托或 NSNotification Center。

    1) 通知中心(最简单的方法)

    在具有显示广告功能的 ViewController 中,在 ViewDidLoad 中添加一个 NSNotificationCenter 观察者

     NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(showAd), name: "ShowInterAdKey", object: nil)
    

    (选择器是要调用的函数,名称是引用这个观察者的关键)

    比在您的 SKScenes 中,当您想要展示广告时,您只需发布通知

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

    2) 有关委托示例,请查看这些文章。

    http://stephenradford.me/creating-a-delegate-in-swift/

    https://www.natashatherobot.com/ios-weak-delegates-swift/

    3) 我还注意到您在展示广告之前没有预加载广告,这不是很有效,并且可能会导致延迟展示广告。

    你应该打电话

      interstital = CreateAd()
    

    在 viewDidLoad 中,它会尽快加载第一个广告。比你应该使用 adMob 委托方法

     func interstitialDidDismissScreen(ad: GADInterstitial!) {
         // load next ad
         interstital = CreateAd()
    

    在当前广告关闭后立即加载下一个广告。

    要使用代理,您可以在 ViewController 中创建扩展

      extension MyViewController: GADInterstitialDelegate {
    
    func interstitialDidReceiveAd(ad: GADInterstitial!) {
        print("AdMob interstitial did receive ad from: \(ad.adNetworkClassName)")
    }
    
    func interstitialWillPresentScreen(ad: GADInterstitial!) {
        print("AdMob interstitial will present")
        // pause game/app
    }
    
    func interstitialWillDismissScreen(ad: GADInterstitial!) {
        print("AdMob interstitial about to be closed")
    }
    
    func interstitialDidDismissScreen(ad: GADInterstitial!) {
        print("AdMob interstitial closed, reloading...")
        interstitial = createAd()
    }
    
    func interstitialWillLeaveApplication(ad: GADInterstitial!) {
        print("AdMob interstitial will leave application")
        // pause game/app
    }
    
    func interstitialDidFailToPresentScreen(ad: GADInterstitial!) {
        print("AdMob interstitial did fail to present")
    }
    
    func interstitial(ad: GADInterstitial!, didFailToReceiveAdWithError error: GADRequestError!) {
        print(error.localizedDescription)
       }
    }
    

    要确保这些被调用,请转到您的 createAd 函数并在返回广告之前添加此行

     let interstitial = ...
     interstitial.delegate = self
    

    最后你的 showAd 方法应该是这样的

      func showAd() {
         print("doing the showadfunc now")
    
        if (interstital.isReady) {
            print("there is an inter ready")
            interstital.presentFromRootViewController(self)
        } else{
            print("The interstitial wasn't ready, reloading again.")
            interstital = CreateAd()
    
        }
     }
    

    4) 如果您正在寻找更可重用的解决方案,您也可以在 gitHub 上查看我的助手。

    https://github.com/crashoverride777/Swift-AdMob-AppLovinTVOS-CustomAds-Helpers

    作为一般提示,您还应该遵循 swift 命名约定,您的 func 和属性应该以小写字母开头。只有类、结构、枚举和协议应该以大写字母开头。 它使您的代码更难为您自己和在 stackoverflow 上阅读,因为它被标记为蓝色但不应该。你有时会做,有时不会。

    希望对你有帮助

    【讨论】:

    • 感谢您提供彻底和早期的反馈回复!我会马上着手解决这个问题,让你(和其他任何人)知道进展如何!
    • 酷,还请检查 Daniel Storm 评论,您的加载也是错误的。先制作广告,然后再展示它。
    • 您还应该更新您的代码以更早地调用 createAd。目前,您正在加载一个新的广告,您想展示一个。您应该在幕后预加载它,以便它们随时准备就绪。
    • 这非常有帮助,非常感谢您所做的一切。你不仅解决了我的问题,而且我也学到了很多东西!如果你们中有任何人有兴趣亲眼看到它,我今晚会提交更新,所以我猜几天后它就会上线。我的应用名为 Frog Leap,bit.ly/frogleapgame
    • 我注意到,现在显示插页式广告时,出于某种原因,它会在屏幕底部生成一个随机广告横幅视图 (admob)。如果有人正在阅读本文并可以提供帮助,那么这是新问题。 stackoverflow.com/questions/37475218/…
    【解决方案2】:

    我敢打赌这是你的问题:

    if (interstital.isReady) {
        print("there is an inter ready")
        interstital.presentFromRootViewController(self)
        interstital = CreateAd()
    

    您正在展示您的插页式广告,然后立即覆盖您的interstitial。您应该实现GADInterstitial 的委托方法并在调用func interstitialDidDismissScreen(ad: GADInterstitial!) 后调用您的func CreateAd() -> GADInterstitial

    【讨论】:

    • 他的主要问题是引用viewController。一旦第一个场景从该属性转移出去,它将为零。你的评论也是真的
    【解决方案3】:

    如果您的设备因以下原因不断崩溃:

    interstitial.delegate = self
    

    然后将其放入您的 CreatAndLoadInterstitial(),而不是您的 viewDidLoad()

    【讨论】:

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