简体   繁体   English

如何在带有Swift的iOS中使用AdMob实施插页式广告?

[英]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. 我正在使用AdMob素材实施插页式广告,但未显示。

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. 为您的类添加委托函数(GADInterstitialDelegate)。

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,  GADInterstitialDelegate {

    var mInterstitial: GADInterstitial!
    var gViewController: UIViewController?

Call loadRequest, it fires interstitialDidReceiveAd when ads ready to show 调用loadRequest,当准备好展示广告时会触发interstitialDidReceiveAd

    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 从任何viewController调用showAdmobInterstitial

            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. 第2步:现在无论您想在何处或任何veiwController中展示插页式广告,然后在该文件中添加这些内容。

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

By this way we can call now interstitial ads easily.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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