Home > database >  How do I hide Interstitial ads after IAP in Swift?
How do I hide Interstitial ads after IAP in Swift?

Time:06-01

I have tried to do it with my ad banner view and it worked perfectly, but I really can't figure out how to do this for my interstitial.

I'm not that skilled so please try to explain it in a very easy and simple way, what my alternative is.

My banner ad view was very easy

if UserDefaults.standard.getActiveRemoveAdsSubscription() == true {
        bannerView.isHidden = true
} else if  UserDefaults.standard.getActiveRemoveAdsSubscription() == true {
    bannerView.isHidden = true
} else {
    bannerView.isHidden = false
}

But what do I do with the InterstitalAd which is not a member of 'isHidden ?

Here is my InterstitalAd code:

import UIKit
import GoogleMobileAds

class UITabBarViewController: UITabBarController, GADFullScreenContentDelegate {

var interstitial: GADInterstitialAd!

let request = GADRequest()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

  
    if UserDefaults.standard.getActiveRemoveAdsSubscription() == true {
        
        
    } else if  UserDefaults.standard.getActiveRemoveAdsSubscription() == true {
        
        
    } else {
        
    }
   
    _ = Timer.scheduledTimer(timeInterval: 30.0, target: self, selector: #selector(timerBlock), userInfo: nil, repeats: true)
    
}

@objc func timerBlock()
{
    print("within the timer!")
                    
    // Active link
    // GADInterstitialAd.load(withAdUnitID: "x", request: request, completionHandler: {ad, error
    
    // Test Link
    GADInterstitialAd.load(withAdUnitID: "ca-app-pub-x", request: request, completionHandler: {ad, error
   
        in
        if let error = error {
            print("Failed to load the ad: \(error)")
            return
        }
        
        self.interstitial = ad
        self.interstitial.fullScreenContentDelegate = self
        
    })
    
    if interstitial != nil{
        interstitial!.present(fromRootViewController: self)
}
    else
    {
        print("Ad didn't load / Wasn't ready")
    }
 
      }

func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
    print("Error: \(error.localizedDescription)")
}

func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
    print("Success!!")
}

func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
    print("User dismissed the ad")
}

CodePudding user response:

There are 2 scenarios to handle this.

  1. Remove GADInterstitialAd right after the purchase.
  2. Check if the Ads are removed on app start.

In both the case, make your interstitial nullable like:
private var interstitial: GADInterstitialAd?

To handle the first case:\

if Purchase.isSuccesful == true {
    interstitial = nil
}

To handle the second case (assuming you are saving the purchase state locally): Inside the timerBlock() function, simply check if the Ads are removed & load Ads accordingly like:

@objc func timerBlock() {
    if UserDefaults.standard.getActiveRemoveAdsSubscription() == false {
        // Load Ad & show Interstitial   
    } else {
         print("Ad Removed, not loading Interstitial.")
    }
}

  • Related