I'm dealing with the Admob interstitials and in particular I'm trying to display an interstitial when a particular ViewController
of my app loads. I used the code provided by the official Admob Interstitial guide but it doesn't work :https://developers.google.com/admob/ios/interstitial?hl=it. I also followed this video here :https://youtu.be/ahmQQ3OeY0Y?t=787 (minute 13.04 stop the video). If you look at the code is the same as in the guide. My objective is to display the ad when the RequestViewController
appears so I try to present the ad in the viewDidAppear
function. Anyway it doesn't work, the console displays this error:
To get test ads on this device, call: request.testDevices = @[ kGADSimulatorID ]
AppDelegate.swift
import UIKit
import UserNotifications
import GoogleMobileAds
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,GADInterstitialDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
GADMobileAds.configure(withApplicationID: "ca-app-pub-*******")
}
}
This is the ViewController where I present the ad:
RequestviewController.swift
class RequestViewController: UIViewController {
var myInterstitial : GADInterstitial?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
//display ad
myInterstitial = createAndLoadInterstitial()
if (myInterstitial?.isReady)!{
print("\n\n\n\n\nReady")
myInterstitial?.present(fromRootViewController: self)
}else { print("\n\n\n\nAd wasn't ready") }
}
func createAndLoadInterstitial()->GADInterstitial {
let interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910") //test Ad unit id
interstitial.delegate = self
interstitial.load(GADRequest())
return interstitial
}
func interstitialDidReceiveAd(ad: GADInterstitial!) {
print("interstitialDidReceiveAd")
}
func interstitial(ad: GADInterstitial!, didFailToReceiveAdWithError error: GADRequestError!) {
print(error.localizedDescription)
}
func interstitialDidDismissScreen(ad: GADInterstitial!) {
print("\n\n\n\n\n\ninterstitialDidDismissScreen")
myInterstitial = createAndLoadInterstitial()
}
you need to write it like,
in place of,