1
votes

I am trying to show an AdMob Interstitial ad every time my game transitions to the GameOver Scene. However, the ad will only appear if I put its initializing function in my viewDidLoad() function in my view controller. I have a notification center set up on the game, and have tried to send a notification upon entering the GameOver Scene, to trigger the function that initializes the ad, yet that did not do the trick. I was wondering how I can trigger it from a scene at any given time instead of showing it immediately upon launch of the app, which is what putting it in my viewDidLoad function of the view controller.

In my GameViewController are these two functions:

public func initAdMobInterstitial() {

    adMobInterstitial = GADInterstitial(adUnitID: AD_MOB_INTERSTITIAL_UNIT_ID)
    adMobInterstitial.delegate = self
    let request = GADRequest()
    request.testDevices = ["ddee708242e437178e994671490c1833"]

    adMobInterstitial.load(request)

}

func interstitialDidReceiveAd(_ ad: GADInterstitial) {

    ad.present(fromRootViewController: self)

}

Here I have commented out initAdMobInterstitial, however when it is uncommented the ad pops up and works properly. This popup occurs as soon as the app launches the first time.

override func viewDidLoad() {
    super.viewDidLoad()

    //initAdMobInterstitial()

    initAdMobBanner()

    NotificationCenter.default.addObserver(self, selector: #selector(self.handle(notification:)), name: NSNotification.Name(rawValue: socialNotificationName), object: nil)

    let scene = Scene_MainMenu(size: CGSize(width: 1024, height: 768))
    let skView = self.view as! SKView

    skView.isMultipleTouchEnabled = true

    skView.ignoresSiblingOrder = true

    scene.scaleMode = .aspectFill

    _ = SGResolution(screenSize: view.bounds.size, canvasSize: scene.size)

    skView.presentScene(scene)

}

Now, in one of my scenes, entitled GameOver, I want the ad to pop up. I would like it to come up every time the scene is presented, so every time the player loses and hits game over. Using the notification center you can see in my view controller class, i have tried to send a notification and have it handled...

override func didMove(to view: SKView) {

    self.sendNotification(named: "interNotif")

}

...by this function, also found in the view controller class

func handle(notification: Notification) {

    if (notification.name == NSNotification.Name(rawValue: interstitialNotificationName)) {

        initAdMobInterstitial()

    }
}

Also as a note, in my view controller I have declared interstitialNotificationName equal to the string "interNotif" to match the notification sent.

1
Please share some code of yours. - Evana

1 Answers

1
votes

Do not present the GADInterstitial as soon as it loads. Your notification func should be presenting it. Then, once the user dismisses the ad request another one. For example:

override func viewDidLoad() {
    super.viewDidLoad()
    // Load the ad 
    initAdMobInterstitial()
}

func interstitialDidReceiveAd(_ ad: GADInterstitial) {
    // Do not present here
    // ad.present(fromRootViewController: self)
}

func handle(notification: Notification) {
    if (notification.name == NSNotification.Name(rawValue: interstitialNotificationName)) {
        // Check if the GADInterstitial is loaded
        if adMobInterstitial.isReady {
            // Loaded so present it
            adMobInterstitial.present(fromRootViewController: self)
        }
    }
}

// Called just after dismissing an interstitial and it has animated off the screen.
func interstitialDidDismissScreen(_ ad: GADInterstitial) {
    // Request new GADInterstitial here
    initAdMobInterstitial()
}

For a complete list of GADInterstitialDelegate ad events refer to AdMob iOS Ad Events.