ios - 如何在 Swift 中使用 admob 插页式广告创建完成处理程序?

标签 ios swift admob

当 admob 的插页式广告正在运行时,我试图暂停我的应用程序(这是一个游戏)的背景。当用户决定关闭广告时,游戏应该重新开始。这最适合使用完成处理程序,但 GoogleMobileAds 似乎没有提供。有没有办法制作一个?

代码会是什么样子:

func gameDidEnd(relish: Relish) {
        view.userInteractionEnabled = false
        scene.stopTicking()

        let score: Int = Int(scoreLabel.text!)!

        if score > userHS {
            updateHighScore(score)
        }

        if (interstitial.isReady) {
            interstitial.presentFromRootViewController(self)
            interstitial = createAd()

            if interstitial.pressedCloseOnAd {
               scene.animateCollapsingLines(relish.removeAllBlocks(), fallenBlocks:
               relish.removeAllBlocks()) {
                relish.beginGame()
                getCurrentReward(user.ID)
            }
        }

        else {
            scene.animateCollapsingLines(relish.removeAllBlocks(), fallenBlocks:
            relish.removeAllBlocks()) {
            relish.beginGame()
            getCurrentReward(user.ID)
        }


    }

此外,我遇到了一个问题,即在他们使用 tappedClose 退出游戏页面后,我尝试显示插页式广告。 .原来,当tappedClose被调用,dismissViewControllerAnimated会被调用。如果我在此之前尝试转换插页式广告,dismissViewControllerAnimated只会关闭广告。如果我将插页式广告放在 dismissViewControllerAnimated 之后,广告永远不会运行(因为它不在窗口层次结构中)。

谢谢你的帮助!

更新
func interstitialWillPresentScreen(ad: GADInterstitial!) {
    view.userInteractionEnabled = false
    scene.stopTicking()
    print("interstitialWillPresentScreen")
}

func interstitialDidDismissScreen(ad: GADInterstitial!, relish: Relish) {
    view.userInteractionEnabled = true
    scene.startTicking()
    getCurrentReward(user.relationshipID)
    print("interstitialDidDismissScreen")
}

func gameDidEnd(relish: Relish) {
    view.userInteractionEnabled = false
    scene.stopTicking()

    let score: Int = Int(scoreLabel.text!)!

    if score > userHS {
        updateHighScore(score)
    }

    if (interstitial.isReady == false) {
        print("is not ready")
        scene.animateCollapsingLines(relish.removeAllBlocks(), fallenBlocks:
        relish.removeAllBlocks()) {
            relish.beginGame()
        }

        getCurrentReward(user.relationshipID)
    }

    else if (interstitial.isReady) {
        print("is ready")
        interstitial.presentFromRootViewController(self)
        interstitialWillPresentScreen(interstitial)
        interstitialDidDismissScreen(interstitial, relish: relish)
        interstitial = createAd()
    }
}

游戏结束时,控制台显示:
is ready
interstitialDidDismissScreen
2016-09-06 22:22:43.915 file[5321:347179] <Google> Request Error: Will not send request because interstitial object has been used.
is not ready

最后一个未准备好表示游戏在后台继续,即使 gameDidEnd被调用,interstitial.isReady是真的,所以它应该只运行 else if陈述。

最佳答案

我认为你可以让你的代码这样工作:

1.将您的插页式广告的代表设置为您的类(class)

2.当您通过此委托(delegate)方法展示插页式广告时停止游戏:

func interstitialWillPresentScreen(ad: GADInterstitial!) {
    // Stop the game
}

3.在用户使用此委托(delegate)方法关闭插页式广告后恢复您的游戏:
func interstitialDidDismissScreen(ad: GADInterstitial!) {
    // Resume the game
}

还有一些GADInterstitialDelegate如果您确实引用此 link,您可能想要检查的方法.

关于ios - 如何在 Swift 中使用 admob 插页式广告创建完成处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39354168/

相关文章:

ios - UIScrollViewKeyboardDismissModeOnDrag 当没有滚动不起作用时

ios - 如何保留对符合 Swift 协议(protocol)的类对象的引用

ios - 以编程方式添加 GADBannerView

ios - GoogleMobileAds (SWIFT) - 由于信号 : Segmentation fault: 11,命令失败

ios - MPPlayableContentDataSource 不调用 contentItemForIdentifier :completionHandler:

ios - 在 Objective-C 中使用 JSON 中的 webService

ios - 将 Swift Delegate 设置为 "self"以外的 View Controller 的语法是什么?

ios - 我遇到了一个奇怪的 SpriteKit AdMob 错误,有点令人困惑

ios - 网站在iOS中的Safari上崩溃

swift - 如何处理 Firebase Auth 错误?/Swift/Firebase