viewDidLoad (AdMob) 上的 Swift 插页式广告

Swift interstitial ad on viewDidLoad (AdMob)(viewDidLoad (AdMob) 上的 Swift 插页式广告)
本文介绍了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 插页式广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)