iosswiftadsinterstitial

Interstitial ads not loading


I am trying to load an interstitial ad, I followed the tutorial and have since checked it a number of times to no avail. I am hoping someone here can see something that I cannot. I have gone through the debug console and I cannot find a solution there. The app isn't crashing, the ad just isn't loading when it is due to.

Below is the first part of my view controller, the ad is implemented in the view did load as bellow. Any help would be much appreciated.

I am calling it appropriately in the app delegate and my info p.list has the required items.

class TodaysMissionViewController: UIViewController, GADFullScreenContentDelegate {

    @IBOutlet weak var MissionText: UITextView!
    var missionText : String = ""
    let remoteConfig = RemoteConfig.remoteConfig()
    let settings = RemoteConfigSettings()

    var interstitial : GADInterstitialAd?

    override func viewDidLoad() {
        super.viewDidLoad()

        let request = GADRequest()
        GADInterstitialAd.load(withAdUnitID: "ca-app-pub-3940256099942544/4411468910",
                                    request: request,
                          completionHandler: { [self] ad, error in
            if let error = error {
              print("Failed to load interstitial ad with error: \(error.localizedDescription)")
              return
            }
            interstitial = ad

            interstitial?.fullScreenContentDelegate = self
        })
        
        configureFirebaseFetch()
        
        MissionText.text = missionText
        
        if interstitial != nil {
            interstitial?.present(fromRootViewController: self)
        } else {
            print("Ad wasn't ready")
        }

        func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
            print("Ad did fail to present full screen content.")
        }

        /// Tells the delegate that the ad will present full screen content.
        func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
            print("Ad will present full screen content.")
        }

        /// Tells the delegate that the ad dismissed full screen content.
        func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
            print("Ad did dismiss full screen content.")
        }
    }

Solution

  • I think your ad is equal to nil when you are trying to present it, that's why it doesn't show up.

    And I believe the reason is that loading an ad takes time, and you are trying to present it before the ad was loaded, as you can see below in your code.

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let request = GADRequest()
        GADInterstitialAd.load(withAdUnitID: "ca-app-pub-3940256099942544/4411468910",
                                    request: request,
                          completionHandler: { [self] ad, error in
            if let error = error {
              print("Failed to load interstitial ad with error: \(error.localizedDescription)")
              return
            }
            interstitial = ad
    
            interstitial?.fullScreenContentDelegate = self
        })
        
        // you're trying to present the ad here, but the compiler might not have entered the completion block above yet
        if interstitial != nil {
            interstitial?.present(fromRootViewController: self)
        } else {
            print("Ad wasn't ready")
        }
    }
    

    I'm not sure presenting an ad straight when the ViewController is loaded is the best practice, still you should be able to solve your issue by moving present() inside the completion block.

        let request = GADRequest()
        GADInterstitialAd.load(withAdUnitID: "ca-app-pub-3940256099942544/4411468910",
                                    request: request,
                          completionHandler: { [self] ad, error in
            if let error = error {
              print("Failed to load interstitial ad with error: \(error.localizedDescription)")
              return
            }
            self.interstitial = ad
            self.interstitial?.fullScreenContentDelegate = self
            self.interstitial?.present(fromRootViewController: self)
        })
    

    I reproduced your issue on a sample project and this solved it, so if it doesn't for you the reason probably lies somewhere else in your project, like the plist.

    On a side note, you should move all these delegate methods outside of viewDidLoad:

        override func viewDidLoad() {
            ...
        }
    
        func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
            print("Ad did fail to present full screen content.")
        }
    
        func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
            print("Ad will present full screen content.")
        }
    
        func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
            print("Ad did dismiss full screen content.")
        }