【问题标题】:Cannot find type 'GADInterstitial' in scope?在范围内找不到类型“GADInterstitial”?
【发布时间】:2021-02-16 14:20:44
【问题描述】:

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

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

我也将此代码添加到 AppDelegate:

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

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

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

【问题讨论】:

标签: ios swift xcode admob


【解决方案1】:

这里是 Admob Interstitial 的代码,只能复制粘贴 我刚刚将 Google 移动广告 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")
      }
}         

}

【讨论】:

    【解决方案2】:

    Google 已在未告知任何人的情况下更新了 SDK。 查看 Admob 上的新指南以添加插页式广告。 本质上,GADInterstitial 已更改为 GADInterterstitialAd,您也必须使用不同的委托。

    感谢 Google。

    【讨论】:

    • Google 因在不告诉任何人、甚至不解释或不提醒任何人的情况下做出更改而臭名昭著。在这方面他们比苹果还差。
    【解决方案3】:

    Google 移动广告 SDK 8.0 的 API 接口发生了巨大变化。请参考谷歌的official document for Interstitial。为了完整起见,这里是对 8.0 之前的 legacy API 的引用。

    主要变化是委托和广告类:

    old new
    GADInterstitialDelegate GADFullScreenContentDelegate
    GADInterstitial GADInterstitialAd

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

    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")
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-24
      • 2021-09-21
      • 2021-08-01
      • 2022-01-16
      • 2021-08-03
      • 2021-11-29
      • 2021-09-23
      • 1970-01-01
      • 2023-01-26
      相关资源
      最近更新 更多