【问题标题】:Swift interstitial ad on viewDidLoad (AdMob)viewDidLoad (AdMob) 上的 Swift 插页式广告
【发布时间】:2016-02-05 04:34:56
【问题描述】:

我正在关注这个tutorial

到目前为止,当我单击按钮时,它已经开始工作了。但是,我似乎无法让它在 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 ];

当我点击按钮时:

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

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

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

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

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

问题

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

【问题讨论】:

  • 从您的interstitialDidDismissScreen 中删除代码并发送新请求,let request = GADRequest() // Requests test ads on test devices. request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"] self.interstitialAd.loadRequest(request)
  • @DanielStorm 那里的广告代码是来自 Google 的默认代码。无需删除。

标签: ios swift admob


【解决方案1】:

在任何viewController中,添加这个函数。

   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声明为成员变量

  var gViewController: UIViewController?
  var mInterstitial: GADInterstitial!

在 AppDelegate 类中添加这些函数

    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。

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GADInterstitialDelegate {

【讨论】:

  • 谢谢。 P.S 那是你自己的谷歌代码吗?请注意,他们的政策是,如果您在测试设备上获得过多点击,他们将禁止您的帐户。
  • @JamesG,感谢您的信息,我刚刚删除了我的广告 ID :)
  • 这种实现对于 admob 策略是不合法的,因为显示添加需要一些时间并且 ViewController 视图已经出现。 @Guru 你有什么办法让它工作吗?
  • @Ángel 从 Admob 获得了任何日志,例如“未显示提供的视图控制器”?解决方案是在 viewDidLoad 中等待 1 秒,然后调用:[self performSelector:@selector(showAdmobInterstitial) withObject:nil afterDelay:1.0];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-21
  • 2018-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多