当 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/