當admob的插頁式廣告正在運行時,我試圖暫停我的應用(這是一款遊戲)的背景。當用戶決定關閉廣告時,遊戲應該繼續。這對於完成處理程序來說效果最好,但GoogleMobileAds似乎沒有提供。有辦法制作一個?如何在Swift中使用admob interstital廣告創建完成處理程序?
代碼將是什麼樣子:
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
聲明。
admob擁有一個名爲'GADInterstitialDelegate'的委託協議,您應該使用'interstitialDidDismissScreen(ad:GADInterstitial)'來查找用戶何時關閉廣告 – Knight0fDragon
@ Knight0fDragon我會在裏面寫些什麼?或者我會將它留空,並用'interstitialDidDismissScreen(interstitial)'替換上面的'if interstitial.pressedClosedAd',讓程序等到它被解散? – user25093
你有'interstitialDidDismissScreen'調用你的完成塊,你只需要將該塊取到視圖控制器 – Knight0fDragon