2016-09-06 55 views
0

當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聲明。

+1

admob擁有一個名爲'GADInterstitialDelegate'的委託協議,您應該使用'interstitialDidDismissScreen(ad:GADInterstitial)'來查找用戶何時關閉廣告 – Knight0fDragon

+0

@ Knight0fDragon我會在裏面寫些什麼?或者我會將它留空,並用'interstitialDidDismissScreen(interstitial)'替換上面的'if interstitial.pressedClosedAd',讓程序等到它被解散? – user25093

+0

你有'interstitialDidDismissScreen'調用你的完成塊,你只需要將該塊取到視圖控制器 – Knight0fDragon

回答

0

你可以讓你的代碼以這種方式工作,我認爲:

1.設置你的內廣告的委託類

2.Stop當你被這個委託方法呈現內廣告的遊戲:

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

3.Resume用戶後,你的遊戲關閉內廣告與此委託方法:

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

如果您確實參考此link,還有一些其他GADInterstitialDelegate方法可能需要檢出。

+0

那麼,如果我想製作插播廣告,並在退出遊戲後投放? 'interstitialWillPresentScreen' - >'//停止遊戲'限制這個功能僅用於遊戲。有沒有一種方法可以讓應用程序只運行該線程並暫停所有其他進程? – user25093

+0

我更新了我的問題,請參考後面的代碼。 – user25093

+0

我在「請求錯誤」問題上添加了一條評論 – Ayazmon

相關問題