在范围内找不到类型 'GADInterstitial'?

Cannot find type 'GADInterstitial' in scope?

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

Cannot find type 'GADInterstitial' in scope

我还将此代码添加到 AppDelegate:

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

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

注意:我也在我的应用中使用了 Firebase Framework。

Google 悄悄更新了SDK。 查看 Admob 上有关添加插页式广告的新指南。 本质上,GADInterstitial 已更改为 GADInterstitialAd,您也必须使用不同的委托。

感谢 Google。

这里是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")
      }
}         

}

Google 移动广告 SDK 8.0 对其 API 界面进行了显着更改。 8.0之前请参考Google的official document for Interstitial. To make it complete, here is the reference to the legacy API

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

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