Search code examples
swiftxcodeadmob

How to make my interstitial ad pop up after x amount of clicks?


I have this code that worked in Objective-C but doesn't work when I converted it to Swift. What I'm trying to do is get an ad to pop up after every 15th click.

import UIKit
import Firebase
import AVFoundation

class Page1: UIViewController, AVAudioPlayerDelegate, GADInterstitialDelegate, UIAlertViewDelegate {
    
    var interstitial: GADInterstitial!
    
    var counter: Int = 0

    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var pageControl: UIPageControl!


    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor(patternImage: UIImage(named: "6splash.png")!)

    
    }


    @IBAction func playAgain(_ sender: Any) {
        if counter % 15 == 0 {
            if interstitial?.isReady != nil {
                interstitial?.present(fromRootViewController: self)
            }
            else {
                
            }
        }
        counter += 1
    }
    
    func createAndLoadInterstitial() {
        interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/1033173712")
        interstitial?.delegate = self
        let request = GADRequest()
        interstitial?.load(request)
    }
    
    func interstitial(_ interstitial: GADInterstitial, didFailToReceiveAdWithError error: GADRequestError) {
        print("interstitialDidFailToReceiveAdWithError: \(error.localizedDescription)")
    }
    
    func interstitialDidDismissScreen(_ interstitial: GADInterstitial) {
        print("interstitialDidDismissScreen")
        createAndLoadInterstitial()
    }

}

I followed Google's tutorial on getting an interstitial to show and that works, but that is for making it pop up every time. When I try my implementation it's not showing ads at all. What am I missing?


Solution

  • Figured it out! It wasn't var counter that was causing the problem like I thought it was, rather it was the AdMob code. My AdMob was caching an ad and showing them once and then not caching/creating new ads, so regardless of the counter or not it will only display one ad. I followed a video tutorial (Geeky Lemon) that showed me how to properly cache and load ads. I then reenabled my var counter code and everything worked like it was supposed to. It shows an ad every 15th click. Here is the code.

    class Page1: UIViewController, AVAudioPlayerDelegate, GADInterstitialDelegate, UIAlertViewDelegate {
        
        
        
        var interstitial: GADInterstitial!
        
        var counter: Int = 0
        
    
        @IBOutlet weak var scrollView: UIScrollView!
        @IBOutlet weak var pageControl: UIPageControl!
        
        
           override func viewDidLoad() {
            super.viewDidLoad()
            self.view.backgroundColor = UIColor(patternImage: UIImage(named: "6splash.png")!)
            
            interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
            
            let request = GADRequest()
            interstitial.load(request)
            
    
        }
    
     
      
        @IBAction func playAgain(_ sender: Any) {
            if counter % 15 == 0 {
                if interstitial.isReady {
                    interstitial.present(fromRootViewController: self)
                    interstitial = CreateAd()
                } else {
                    print("Ad wasn't ready")
                }
            }
            counter += 1
        }
    
        
    
       }
    
    func CreateAd() -> GADInterstitial {
        let interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
        interstitial.load(GADRequest())
        return interstitial
    }

    Literally struggled for days. Turns out I was looking at the wrong place the whole time. I thought for sure it had to be the var counter because I converted those snippets from objective-c and followed Google's very own Swift tutorial for how to integrate their sdk. I was just loading a single ad and that's why I thought the counter was broken.