Ios 如何在Swift中使用admob Intersital ads创建完成处理程序?
当admob的中间广告运行时,我试图暂停我的应用程序(这是一个游戏)的背景。当用户决定关闭广告时,游戏应该继续。这对于一个完成处理程序来说是最好的,但是Google MobileADS似乎没有提供。有办法做一个吗 代码是什么样子的:Ios 如何在Swift中使用admob Intersital ads创建完成处理程序?,ios,swift,admob,Ios,Swift,Admob,当admob的中间广告运行时,我试图暂停我的应用程序(这是一个游戏)的背景。当用户决定关闭广告时,游戏应该继续。这对于一个完成处理程序来说是最好的,但是Google MobileADS似乎没有提供。有办法做一个吗 代码是什么样子的: func gameDidEnd(relish: Relish) { view.userInteractionEnabled = false scene.stopTicking() let score: Int = I
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
准备好了
间质脱细胞筛选
2016-09-06 22:22:43.915文件[5321:347179]请求错误:将不发送请求,因为已使用填隙对象。
他还没准备好
最后一个未准备好表示游戏在后台继续进行,即使调用了
gameDidEnd
时,interstitual.isReady
是真的,所以它应该只运行else if
语句。您可以这样使代码工作,我认为:
1.将你的洲际广告委托给你的班级
2.当您通过此代理方式展示Interstail广告时,停止游戏:
func interstitialWillPresentScreen(ad: GADInterstitial!) {
// Stop the game
}
func interstitialDidDismissScreen(ad: GADInterstitial!) {
// Resume the game
}
3.在用户使用此代理方法关闭Interstail广告后继续游戏:
func interstitialWillPresentScreen(ad: GADInterstitial!) {
// Stop the game
}
func interstitialDidDismissScreen(ad: GADInterstitial!) {
// Resume the game
}
如果您确实参考了这篇文章,您可能还想签出一些其他方法。admob有一个名为
GadInterstitutialDelegate
的委托协议,您应该使用InterstitutionAlDidDismissScreen(ad:GadInterstitutial)
查找用户何时关闭ad@Knight0fDragon我会在里面写些什么?或者我会将其留空,并将上面的替换为interstitual。按ClosedDad
替换为interstitutial-IDDISMISSCREEN(interstitual)
,使程序等待它被解除吗?您有interstitutial-IDDISMISSCREEN
调用您的完成块,您只需将该块交给视图控制器,您之所以会出现此请求错误
,是因为您已经使用了要呈现的Interstail。这意味着您已经在此Interstal对象上调用了函数presentFromRootController
。你需要更新你的Intersial对象,如果我想在他们退出游戏后制作一个插播广告呢interstitialWillPresentScreen
->//停止游戏
将此功能的使用限制为仅在游戏中使用。是否有办法使应用程序只运行该线程并暂停所有其他进程?我更新了我的问题,请参考行后的代码。我对请求错误问题添加了评论