Reputation:
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)
}
}
Upvotes: 1
Views: 1422
Reputation: 22042
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()
Upvotes: 1
Reputation:
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.
Upvotes: 0