Cannot find type 'GADInterstitial' in scope?
Here this is the code of Admob Interstitial only copy and pasteI just upgraded Google Mobile Ads SDK to version 8.0
import GoogleMobileAdsclass 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 Mobile Ads SDK 8.0 has a dramatic change of its API interfaces. Please refer to Google's official document for Interstitial. To make it complete, here is the reference to the legacy API prior to 8.0.
The main changes are the delegate and the Ad class:
old | new |
---|---|
GADInterstitialDelegate | GADFullScreenContentDelegate |
GADInterstitial | GADInterstitialAd |
And instead of initializing the Ad in the legacy way:
interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")let request = GADRequest()interstitial.load(request)
it's now initialized in the following way
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 }
And the new GADFullScreenContentDelegate methods:
/// 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.")}
Note there is no longer the interstitialDidReceiveAd
method. Instead you either start to present the Ad in the GADInterstitialAd.load()
completion call back, or at a later stage to present the Ad if it is initialized:
if interstitial != nil { interstitial.present(fromRootViewController: self)} else { print("Ad wasn't ready")}