首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用AdMob和Swift在iOS中实现间质广告?

如何使用AdMob和Swift在iOS中实现间质广告?
EN

Stack Overflow用户
提问于 2016-02-12 07:59:22
回答 2查看 1.3K关注 0票数 1

我正在使用AdMob实现间隙广告,但它没有显示。

这是我的课,我想在那里实现间质广告。

代码语言:javascript
复制
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)
        }
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-12 09:07:19

为类添加委托函数(GADInterstitialDelegate)。

代码语言:javascript
复制
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,  GADInterstitialDelegate {

    var mInterstitial: GADInterstitial!
    var gViewController: UIViewController?

调用loadRequest,当广告准备显示时,它会触发interstitialDidReceiveAd

代码语言:javascript
复制
    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)
    }

从任意showAdmobInterstitial调用viewController

代码语言:javascript
复制
            let App = UIApplication.sharedApplication().delegate as! AppDelegate
            App.gViewController = self;
            App.showAdmobInterstitial()
票数 1
EN

Stack Overflow用户

发布于 2016-02-15 06:04:07

代码语言:javascript
复制
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)
    }
//-------------------------

第二步:现在,无论在哪里,或者在任何veiwController中,您都希望显示间隙广告,然后在该文件中添加这些内容。

代码语言:javascript
复制
let App = UIApplication.sharedApplication().delegate as! AppDelegate
            App.gViewController = self;
            App.showAdmobInterstitial()

By this way we can call now interstitial ads easily.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35357479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档