如何在Swift中使用admob interstital广告创建完成处理程序?

时间:2016-09-06 16:53:12

标签: ios swift admob

当admob的插页式广告正在投放时,我正试图暂停我的应用(这是一款游戏)的背景。当用户决定关闭广告时,游戏应该恢复。这对于完成处理程序最有效,但GoogleMobileAds似乎没有提供。有没有办法制作一个?

代码的样子:

Book

此外,我遇到的问题是,在我们使用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语句。

最后一个未准备好表示游戏在后台继续进行。 / p>

1 个答案:

答案 0 :(得分:0)

您可以按照我的想法使您的代码正常工作:

1.将您的intertial广告代表设置为您的班级

2.当您通过此委托方式展示最终广告时停止游戏:

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

3.用户使用此委托方法关闭内部广告后重新启动游戏:

func interstitialDidDismissScreen(ad: GADInterstitial!) {
    // Resume the game
}

如果您参考此link,您可能需要查看其他一些GADInterstitialDelegate方法。