如何使用Swift在iOS中使用AdMob实现插页式广告?

时间:2016-02-12 07:59:23

标签: ios swift2 admob

我正在使用AdMob内容实施插页式广告,但它没有显示。

这是我想要实施插页式广告的课程。

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)
        }
    }

2 个答案:

答案 0 :(得分:0)

为您的班级添加委托功能(GADInterstitialDelegate)。

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,  GADInterstitialDelegate {

    var mInterstitial: GADInterstitial!
    var gViewController: UIViewController?

调用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)
    }

从任何viewController调用showAdmobInterstitial

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

答案 1 :(得分:0)

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)
    }
//-------------------------

第2步:现在,无论您想在哪个veiwController中显示插页式广告,都要在该文件中添加此内容。

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

By this way we can call now interstitial ads easily.