Search code examples
iosswiftfirebaseadmobinterstitial

how can I make my interstitial adMob ad show overtime a button is clicked


Right now it only shows the full page ad one time. When I go back and press the button again it does not show. I would like it to show overtime my button is clicked. I have my AdMob hooked up to my Firebase project.

class FirstViewController: UIViewController, UIAlertViewDelegate {


var interstitial: GADInterstitial!

@IBAction func loadAd(_ sender: Any) {

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

override func viewDidLoad() {

    interstitial = GADInterstitial(adUnitID: "ca-app-pub-11111111/9758796532")
    let request = GADRequest()
    interstitial.load(request)

    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}


Solution

  • You need to create GAD request every time.

    GADInterstitial is a one time use object. That means once an interstitial is shown, hasBeenUsed returns true and the interstitial can't be used to load another ad. To request another interstitial, you'll need to create a new GADInterstitial object. The best practice, as shown above, is to have a helper method to handle creating and loading an interstitial.

     @IBAction func loadAd(_ sender: Any) {
             interstitial = GADInterstitial(adUnitID: "ca-app-pub-11111111/9758796532")
            interstitial.delegate = self
            let request = GADRequest()
            interstitial.load(request)
    
        }
    

    And while loaded successfully you need to present at delegate method

    optional func interstitialDidReceiveAd(_ ad: GADInterstitial!){
        if (interstitial.isReady){
               interstitial.present(fromRootViewController: self)
             }
    }
    
    optional func interstitialWillDismissScreen(_ ad: GADInterstitial!){
          interstitial.delegate = nil
          interstitial = nil
     } 
    
     optional func interstitialDidFail(toPresentScreen ad: GADInterstitial!){
       interstitial.delegate = nil
       interstitial = nil
      }
    

    More : https://firebase.google.com/docs/admob/ios/interstitial