每次呈现视图控制器时呈现插页式广告 - Swift 4

Present Interstitial Ad every time the View Controller is presented - Swift 4

我只想在每次出现某个视图控制器时出现一个插页式广告。这是相关代码(不是全部)...

//import ads, set delegate, and declare variable...
import UIKit
import GoogleMobileAds

class myVC: UIViewController, GADInterstitialDelegate {

var interstitial: GADInterstitial!

准备好广告并在显示视图控制器时显示它...

override func viewDidLoad() {
    super.viewDidLoad()

    interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
    let request = GADRequest()
    interstitial.load(request)
}


override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    if (interstitial.isReady) {
        interstitial.present(fromRootViewController: self)
        interstitial = createAd()
    }
}

确保广告为下次准备好...

func createAd() -> GADInterstitial {

    let inter = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")

    inter.load(GADRequest())
    return inter
}

为什么这不起作用?我可以通过按一个按钮来显示广告(我显然为此稍微修改了代码),但我希望广告仅与视图控制器上的演示文稿一起显示。我错过了什么?

我注意到 interstitial ad 的问题是在调用 viewWillAppearviewDidAppear 时它还没有准备好。因此,对 interstitial.isReady 的检查失败。 GADInterstitialDelegate 方法调用了 interstitialDidReceiveAd ,它将在添加准备就绪时告诉您。然后,您需要构建一些逻辑来监控广告何时准备就绪,以及您是否已经在向用户展示广告。

import UIKit
import GoogleMobileAds

class AdViewController:UIViewController, GADInterstitialDelegate {

    var interstitial: GADInterstitial!

    private var shouldDisplayAd = true

    private var isAdReady:Bool = false {
        didSet {
            if isAdReady && shouldDisplayAd {
                displayAd()
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        print(#function)
        interstitial = GADInterstitial(adUnitID: "/6499/example/interstitial")
        interstitial.delegate = self
        let request = GADRequest()
        interstitial.load(request)
        print(#function, "shouldDisplayAd", self.shouldDisplayAd, "isAdReady", self.isAdReady)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        print(#function, "shouldDisplayAd", self.shouldDisplayAd, "isAdReady", self.isAdReady)
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        print(#function, "shouldDisplayAd", self.shouldDisplayAd, "isAdReady", self.isAdReady)
    }

    @IBAction func adButton(_ sender: UIButton) {
        print(#function, "shouldDisplayAd", self.shouldDisplayAd, "isAdReady", self.isAdReady)
        displayAd()
    }

    private func displayAd() {
        print(#function, "ad ready", interstitial.isReady)
        if (interstitial.isReady) {
            shouldDisplayAd = false
            interstitial.present(fromRootViewController: self)
        }
    }

    private func presentViewController() {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let myVC = storyboard.instantiateViewController(withIdentifier: "buttonViewController")
        self.present(myVC, animated: true) { [unowned self] in
            self.shouldDisplayAd = true
            print(#function, "shouldDisplayAd", self.shouldDisplayAd, "isAdReady", self.isAdReady)
        }
    }

    func createAndLoadInterstitial() -> GADInterstitial {
        interstitial = GADInterstitial(adUnitID: "/6499/example/interstitial")
        interstitial.delegate = self
        interstitial.load(GADRequest())
        shouldDisplayAd = false
        return interstitial
    }

    /// Tells the delegate an ad request failed.
    func interstitialDidFail(toPresentScreen ad: GADInterstitial) {
        print(#function, "ad ready", interstitial.isReady)
    }

    func interstitialDidReceiveAd(_ ad: GADInterstitial) {
        print(#function, "ad ready", interstitial.isReady)
        isAdReady = true
    }

    //Tells the delegate the interstitial is to be animated off the screen.
    func interstitialWillDismissScreen(_ ad: GADInterstitial) {
        print("interstitialWillDismissScreen")
    }

    //Tells the delegate the interstitial had been animated off the screen.
    func interstitialDidDismissScreen(_ ad: GADInterstitial) {
        print("interstitialDidDismissScreen")
        presentViewController()
        interstitial = createAndLoadInterstitial()
        print(#function, "shouldDisplayAd", shouldDisplayAd, "isAdReady", isAdReady)
    }
}