swiftadmobinterstitialviewwillappear

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


I simply want an Interstitial Ad to appear each time a certain View Controller appears. Here's the relevent code (not everything) ...

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

class myVC: UIViewController, GADInterstitialDelegate {

var interstitial: GADInterstitial!

Get the ad ready and present it when the View Controller is presented ...

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()
    }
}

Ensure the ads will be ready for next time ...

func createAd() -> GADInterstitial {

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

    inter.load(GADRequest())
    return inter
}

So why isn't this working? I can get the ad to show by pressing a button (I obviously alter the code a little to do that), but I want the ad to simply appear with the presentation on the View Controller. What am I missing?


Solution

  • The issue that I noticed with the interstitial ad is that it is not ready when viewWillAppear or viewDidAppear is called. Because of this the check on interstitial.isReady fails. The GADInterstitialDelegate method has called interstitialDidReceiveAd that will tell you when the add is ready. You then need to build some logic around monitoring when an ad is ready, and if you are already displaying an ad to the user.

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