如何在点击后退按钮时显示一次 AdMob Interstitial

How to show AdMob Interstitial one time on back button tap

我有这个代码:

func createAndLoadInterstitial()  {
    interstitial = GADInterstitial(adUnitID: "ca-app-pub-xxxxxxxxxx/xxxxx")
    interstitial.delegate = self
    let request = GADRequest()
    interstitial.loadRequest(request)
}

override func viewDidAppear(animated: Bool) {
    if (interstitial.isReady && showAd) {
        showAd = false
        // print("iterstitialMain is ready")
        interstitial.presentFromRootViewController(self)
        self.createAndLoadInterstitial()
    }
    else {
        showAd = true
    }
}

并且有效。但每次用户点击后退按钮时它都会显示一个广告。

我只想在用户点击后退按钮时显示一次广告。过一段时间再展示广告会更好吗?例如,每 5 分钟?

您最初在哪里将 showAd 设置为 true?您 if 语句中的逻辑是主要问题。

if 语句中设置 showAd = false 后,下次调用 viewDidAppear 时将执行 else 语句,设置 showAdtrue.

您应该做的是检查您的 Bool,然后检查 interstitial.isReady。然后,在您的 GADInterstitialinterstitialDidDismissScreen 委托方法中,您将更新您的 showAd Bool 并请求另一个 GADInterstitial 如果您愿意。你说你只想显示一个 GADInterstitial 所以没有必要请求另一个。例如:

override func viewDidAppear(animated: Bool) {
    if showAd { // Should we show an ad?
        if interstitial.isReady { // Is the ad ready?
            interstitial.presentFromRootViewController(self)
        }
    }
    else {
        // Do nothing
    }
}

func interstitialDidDismissScreen(ad: GADInterstitial!) {
    // Ad was presented and dismissed
    print("interstitialDidDismissScreen")
    showAd = false // Don't show anymore ads
}

此外,您可以更改:

let request = GADRequest()
interstitial.loadRequest(request)

只是:

interstitial.loadRequest(GADRequest())

在你的 createAndLoadInterstitial 函数中。

回答问题的第二部分,询问您是否应该在延迟一段时间后展示广告,这违反了 AdMob 计划政策。

不合规实施示例:

  • Interstitial ads that appear before the app has opened or after the app has closed.
  • Interstitial ads that are triggered after a user closes another interstitial ad.
  • Interstitial ads loading unexpectedly while a user is viewing the app’s content. Remember to only serve interstitials between pages of content.
  • Interstitial ads that trigger after every user click.
  • Interstitial ads that appear during periods of game play or heavy user interaction.