每次展示View Controller时展示非页内广告-Swift 4

时间:2019-01-26 17:25:55

标签: swift admob interstitial viewwillappear

我只是希望在每次特定View Controller出现时都显示插页式广告。这是相关的代码(不是全部)...

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

class myVC: UIViewController, GADInterstitialDelegate {

var interstitial: GADInterstitial!

准备好广告并在展示View Controller时展示它...

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
}

那为什么不起作用?我可以通过按一个按钮来显示广告(显然,我为此做了一些改动),但是我希望广告可以与演示文稿一起简单地显示在View Controller上。我想念什么?

1 个答案:

答案 0 :(得分:0)

我在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)
    }
}