Besart
Besart

Reputation: 399

Cannot find type 'GADInterstitial' in scope?

I just upgraded Google Mobile Ads SDK to version 8.0, but I am getting this error:

Cannot find type 'GADInterstitial' in scope

I also added this code to AppDelegate:

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

Google Mobile Ads SDK worked perfectly until I upgraded to version 8.0

Note: I also use Firebase Framework in my app.

Upvotes: 18

Views: 11628

Answers (3)

M Hamayun zeb
M Hamayun zeb

Reputation: 518

Here this is the code of Admob Interstitial only copy and paste I just upgraded Google Mobile Ads SDK to version 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")
      }
}         

}

Upvotes: 17

CodeBrew
CodeBrew

Reputation: 7187

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

Upvotes: 14

RobD
RobD

Reputation: 141

Google have updated the SDK without telling anyone. Take a look at the new guide on Admob for adding an interstitial. Essentially GADInterstitial has changed to GADInterterstitialAd and you have to use a different Delegate too.

Thanks for that Google.

Upvotes: 7

Related Questions