在范围内找不到类型“GADInterstitial”?

我刚刚将 Google 移动广告 SDK 升级到 8.0 版,但出现此错误:

在范围内找不到类型“GADInterstitial”

我还将此代码添加到 AppDelegate:

GADMobileAds.sharedInstance().start(completionHandler: nil)

在我升级到 8.0 版之前,Google 移动广告 SDK 运行良好

注意:我还在我的应用中使用 Firebase 框架。

回答

这是 Admob Interstitial 的代码 仅复制和粘贴 我刚刚将 Google Mobile Ads SDK 升级到 8.0 版

import GoogleMobileAds

class ViewController: UIViewController,GADFullScreenContentDelegate{
    
private var interstitial: GADInterstitialAd?
    
override func viewDidLoad() {
    super.viewDidLoad()
    
    let request = GADRequest()
        GADInterstitialAd.load(withAdUnitID:"ca-app-pub-3940256099942544/4411468910",request: request,
        completionHandler: { [self] ad, error in
          if let error = error {
             return
             }
             interstitial = ad
             interstitial?.fullScreenContentDelegate = self
            }
        )
}

 func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
   print("Ad did fail to present full screen content.")
 }

 func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
   print("Ad did present full screen content.")
 }

 func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
   print("Ad did dismiss full screen content.")
 }   

@IBAction func AddAction(_ sender: UIButton) {        
    if interstitial != nil {
        interstitial.present(fromRootViewController: self)
      } else {
        print("Ad wasn't ready")
      }
}         

}


回答

谷歌在没有告诉任何人的情况下更新了 SDK。查看 Admob 上关于添加插页式广告的新指南。本质上 GADInterstitial 已更改为 GADInterstitialAd,您也必须使用不同的 Delegate。

谢谢你的谷歌。


回答

Google 移动广告 SDK 8.0 的 API 接口发生了巨大变化。请参阅 Google 的Interstitial 官方文档。为了使其完整,这里是对 8.0 之前的旧 API的参考。

主要的变化是委托和 Ad 类:

老的 新的
GADInterstitialDelegate GADFullScreenContentDelegate
GAD插页式广告 GAD 插页式广告

而不是以传统方式初始化广告:

interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
let request = GADRequest()
interstitial.load(request)

它现在以下列方式初始化

let request = GADRequest()
GADInterstitialAd.load(withAdUnitID:"ca-app-pub-3940256099942544/4411468910",
                            request: request,
                  completionHandler: { [self] ad, error in
                    if let error = error {
                      print("Failed to load interstitial ad with error: (error.localizedDescription)")
                      return
                    }
                    interstitial = ad
                    interstitial?.delegate = self
                  }

以及新的 GADFullScreenContentDelegate 方法:

/// Tells the delegate that the ad failed to present full screen content.
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
  print("Ad did fail to present full screen content.")
}

/// Tells the delegate that the ad presented full screen content.
func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
  print("Ad did present full screen content.")
}

/// Tells the delegate that the ad dismissed full screen content.
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
  print("Ad did dismiss full screen content.")
}

请注意,不再有该interstitialDidReceiveAd方法。相反,您要么开始在GADInterstitialAd.load()完成回调中展示广告,要么在稍后阶段展示已初始化的广告:

if interstitial != nil {
  interstitial.present(fromRootViewController: self)
} else {
  print("Ad wasn't ready")
}


以上是在范围内找不到类型“GADInterstitial”?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>