Search code examples
swiftsprite-kitadmobskscenefirebase-admob

How to display AdMob Banner Ad on SKScene in SpriteKit?


I want to display an AdMob Banner Ad over an SKScene. Right now, I have the following code in the GameViewController, but nothing is appearing in the SKScene. Do I need to include code in the didMove method of the SKScene, or am I just missing something in my GameViewController?

import UIKit
import SpriteKit
import GoogleMobileAds

class GameViewController: UIViewController {

    var bannerView: GADBannerView!
    
    override var prefersStatusBarHidden: Bool {
        return true
    }
    override func viewDidLoad() {
        if view.frame.height > 736 {
            Screen.hasNotch = true
        }
        super.viewDidLoad()
        if let view = self.view as! SKView? {
            // Load the SKScene from 'GameScene.sks'
            let scene = MenuScene(size: UIScreen.main.bounds.size)
            // Set the scale mode to scale to fit the window
            scene.scaleMode = .fill
            // Present the scene
            view.presentScene(scene)
            view.ignoresSiblingOrder = true
            view.showsFPS = false
            view.showsNodeCount = false
        }
        bannerView = GADBannerView(adSize: kGADAdSizeBanner)
        addBannerViewToView(bannerView)
        bannerView.adUnitID = AdMob.bannerid
        bannerView.rootViewController = self
        bannerView.load(GADRequest())
    }

//MARK: - AdMob
    
    func adViewDidReceiveAd(_ bannerView: GADBannerView) {
      bannerView.alpha = 0
      UIView.animate(withDuration: 1, animations: {
        bannerView.alpha = 1
      })
    }
    
    func addBannerViewToView(_ bannerView: GADBannerView) {
        bannerView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(bannerView)
        view.addConstraints(
          [NSLayoutConstraint(item: bannerView,
                              attribute: .bottom,
                              relatedBy: .equal,
                              toItem: view.safeAreaLayoutGuide.bottomAnchor,
                              attribute: .top,
                              multiplier: 1,
                              constant: 0),
           NSLayoutConstraint(item: bannerView,
                              attribute: .centerX,
                              relatedBy: .equal,
                              toItem: view,
                              attribute: .centerX,
                              multiplier: 1,
                              constant: 0)
          ])
    }
    
}

Solution

  • This problem has been resolved. I had not entered my billing information on my Admob account, so my account verification had not been processed yet. This resulted in an ad not appearing. If you are having the same issue, I recommend:

    1. registering your device as a test device
    2. Checking your code with a test ad

    If the test ad appears, make sure that you have taken the required measures to get your account verified. The above code works perfectly when attempting to display ads on an SKScene.