viewDidLoad (AdMob) 上的 Swift 插页式广告 [英] Swift interstitial ad on viewDidLoad (AdMob)

查看:21
本文介绍了viewDidLoad (AdMob) 上的 Swift 插页式广告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注这个 教程

到目前为止,当我单击按钮时,它已经开始工作了.但是,我似乎无法让它在 viewDidLoad 上显示插页式广告.

So far I have got it to work when I click the button. However I cannot seem to get it to show an interstitial ad on viewDidLoad.

这就是我所拥有的:

override func viewDidLoad() {
  super.viewDidLoad()
  self.interstitialAd = GADInterstitial(adUnitID: "ADUnitID")

  let request = GADRequest()

  // Requests test ads on test devices.
  request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]

  self.interstitialAd.loadRequest(request)
  self.interstitialAd = reloadInterstitialAd()
}

控制台不断向我显示:

2016-02-04 22:27:51.855 GoogleAdMobTutorial[3394:56415] 要在此设备上获取测试广告,请调用:request.testDevices = @[ kGADSimulatorID ];

2016-02-04 22:27:51.855 GoogleAdMobTutorial[3394:56415] To get test ads on this device, call: request.testDevices = @[ kGADSimulatorID ];

当我点击按钮时:

@IBAction func showAd(sender: AnyObject) {
    if self.interstitialAd.isReady {
        self.interstitialAd.presentFromRootViewController(self)
    }
}

广告出现了.当我关闭广告时,我会在控制台中看到:

The ad shows up. When I dismiss the ad, I get this in the console:

2016-02-04 22:29:16.661 GoogleAdMobTutorial[3394:56743] 请求错误:不会发送请求,因为已使用插页式对象.

2016-02-04 22:29:16.661 GoogleAdMobTutorial[3394:56743] Request Error: Will not send request because interstitial object has been used.

这是我关闭广告时调用的函数:

This is the function that is called when I dismiss the ad:

func interstitialDidDismissScreen(ad: GADInterstitial!) {
    self.interstitialAd = reloadInterstitialAd()
}

问题

为了清楚起见:如何在视图控制器加载时显示插页式广告?

Question

Just for clarity: How do I get an interstitial ad to show when the viewcontroller loads?

推荐答案

在任意一个viewController中,添加这个函数.

In any viewController, add this function.

   override func viewDidLoad() {
    super.viewDidLoad()

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

// Try below in iOS 14+ and comment above line
// var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: App, selector: Selector("showAdmobInterstitial"), userInfo: nil, repeats: false)
}

//在AppDelegate中,将gViewController声明为成员变量

// In AppDelegate, declare gViewController as member variable

  var gViewController: UIViewController?
  var mInterstitial: GADInterstitial!

在 AppDelegate 类中添加这些函数

Add these function in AppDelegate class

    func showAdmobInterstitial()
    {
            let kGoogleFullScreenAppUnitID = "ca-app-pub-***";
            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)
    }

确保您在 AppDelegate 中添加了 GADInterstitialDelegate.

make sure you added GADInterstitialDelegate in AppDelegate.

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GADInterstitialDelegate {

这篇关于viewDidLoad (AdMob) 上的 Swift 插页式广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆