Admob interstitial doesn't work

982 views Asked by At

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()
}
2

There are 2 answers

1
Jigar Tarsariya On

you need to write it like,

appDelegate.createAndLoadInterstitial()

in place of,

appDelegate.myInterstitial?.present(fromRootViewController: self)
0
RedBrogdon On

It looks like the root problem is this section of RequestViewController:

myInterstitial = createAndLoadInterstitial()

if (myInterstitial?.isReady)!{
    print("\n\n\n\n\nReady")
    myInterstitial?.present(fromRootViewController: self)
}else { print("\n\n\n\nAd wasn't ready") }

Interstitials are loaded asynchronously, which means the call to load() will return before the ad is loaded and ready to display. If your app is calling load() in createAndLoadInterstitial and then checking isReady immediately afterwards, there's no time for the ad to actually load, and isReady will always be false.

A good way to deal with this could be to create the interstitial in your first view controller, and then show it before transitioning to RequestViewController. That would give the ad time to load, and would still display it during the same transition.

FWIW, transition time between ViewControllers is a great place in the flow of your app to use an interstitial, so well done there.

Also, the line:

To get test ads on this device, call: request.testDevices = @[ kGADSimulatorID ]

isn't an error. The Android and iOS SDKs always print that to the log so publishers will know how to get test ads for a particular device or emulator. If you were running on a hardware device instead of the iOS simulator, you'd see a unique identifier for the device in that line, which you could then use in your code to get test ads.