Step 1: Request for Ads
To request ads, use the following method:
func requestForAdsInter(view: UIViewController) {
GADInterstitialManager.shared.loadInterstitialAds { [weak self] didCompleted in
debugPrint(didCompleted)
self?.getNativeAdControllerInter(view: view)
} didCompleteWithError: { didCompleteWithError in
debugPrint("didCompleteWithError \(didCompleteWithError)")
}
}
func loadInterstitialAds(didCompleted: @escaping successHandlerInter,
didCompleteWithError: @escaping errorHandlerInter) -> () {
#if DEBUG
GADMobileAds.sharedInstance().requestConfiguration.testDeviceIdentifiers = [ testDeviceID ]
#endif
let adUnitID = getInterstitialAdUnitID()
GADInterstitialAd.load(withAdUnitID: adUnitID,
request: GADRequest(),
completionHandler: { [self] ad, error in
if let error = error {
didCompleteWithError(error.localizedDescription)
return
}
interstitial = ad
interstitial?.fullScreenContentDelegate = self
didCompleted(true)
})
}
- adUnitID: Interstitial ad unit id which you got from the google console.
- viewController: where the interstitial will be visible.
Step 2: Load Ad method
method for load the ads.
func getNativeAdControllerInter(view: UIViewController) {
GADInterstitialManager.shared.showInterstitialAds(
view: view,
didPresent: {
debugPrint("In Present")
},
didDismiss: {
debugPrint("didDismiss")
},
didFailToPresent: { didFailToPresentWithError in
debugPrint("didFailToPresent")
})
}
func showInterstitialAds(view: UIViewController,
didPresent: @escaping didPresentFullScreenContent,
didDismiss: @escaping didDismissFullScreenContent,
didFailToPresent: @escaping didFailToPresentFullScreenContentWithError) -> () {
presentFullScreenContent = didPresent
dismissFullScreenContent = didDismiss
failToPresentFullScreenContentWithError = didFailToPresent
if let ad = interstitial {
ad.present(fromRootViewController: view)
} else {
print("Ads was not ready")
}
}
Step 3: Delegate methods
GADFullScreenContentDelegate will provide the callbacks protocol method for GAD request for Interstitial ads
func ad(_ ad: GADFullScreenPresentingAd,
didFailToPresentFullScreenContentWithError error: Error) {
print("Ads Fail to Present")
}
func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
presentFullScreenContent?()
}
func adWillDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
dismissFullScreenContent?()
}