简体   繁体   中英

How to implement interstitial Ads using AdMob in iOS with Swift?

I'm implementing the interstitial Ads using the AdMob stuff but it is not displaying.

This is my class where I want to implement interstitial ads.

import UIKit
import GoogleMobileAds

class SearchNew: UIViewController{

    var interstitial: GADInterstitial!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")

        let request = GADRequest()
        request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
        self.interstitial.loadRequest(request)

        self.showAd()
    }

    func showAd() {
        if self.interstitial.isReady {
            self.interstitial.presentFromRootViewController(self)
        }
    }

Add delegate function (GADInterstitialDelegate) for your class.

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,  GADInterstitialDelegate {

    var mInterstitial: GADInterstitial!
    var gViewController: UIViewController?

Call loadRequest, it fires interstitialDidReceiveAd when ads ready to show

    func showAdmobInterstitial()
    {
            self.mInterstitial = GADInterstitial.init(adUnitID:kGoogleFullScreenAppUnitID )

            mInterstitial.delegate = self
            let Request  = GADRequest()
            Request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
            mInterstitial.loadRequest(Request)
    }

    func interstitialDidReceiveAd(ad: GADInterstitial!)
    {
        ad.presentFromRootViewController(self.gViewController)
    }

Call showAdmobInterstitial from any viewController

            let App = UIApplication.sharedApplication().delegate as! AppDelegate
            App.gViewController = self;
            App.showAdmobInterstitial()
In this way I did this admob Interstitial ads task

import UIKit
import GoogleMobileAds

//-------Interstitial adds--------
Step 1: You need to add this stuff in side your AppDelegate.swift file

@UIApplicationMain
Delegate: UIResponder, UIApplicationDelegate,GADInterstitialDelegate {

    var gViewController: UIViewController?

    var window: UIWindow?


 func showAdmobInterstitial()
    {
        let kGoogleFullScreenAppUnitID = "ca-app-pub-3940256099942544/4411468910";
        self.mInterstitial = GADInterstitial.init(adUnitID:kGoogleFullScreenAppUnitID )

        mInterstitial.delegate = self
        let Request  = GADRequest()
        Request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
        mInterstitial.loadRequest(Request)
    }

    func interstitialDidReceiveAd(ad: GADInterstitial!)
    {
        ad.presentFromRootViewController(self.gViewController)
    }
//-------------------------

Step 2: Now where ever or in any veiwController you want to show interstitial ads then add this stuff within that file.

let App = UIApplication.sharedApplication().delegate as! AppDelegate
            App.gViewController = self;
            App.showAdmobInterstitial()

By this way we can call now interstitial ads easily.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM