Search code examples
iosfirebasesprite-kitadmobswift3

AdMob Banner Ad not Showing Swift 3


I am working on a SpriteKit game and am implementing banner ads via AdMob (Google Firebase). I have followed Google's tutorials here and here on setting up your first ad request, but when I run the app on either the sim or my device (iPhone 6s), the banner does not show up. Per the tutorials, I implemented the banner view via the Storyboard and the rest was set up programmatically. Below is the code of my view controller.

Q: Why isn't my banner showing?

Follow-up Q: Is it possible to show the banner only on certain SKScenes?

import UIKit
import SpriteKit
import GoogleMobileAds

class GameViewController: UIViewController, GADBannerViewDelegate {

    @IBOutlet weak var bannerView: GADBannerView!

    override func viewDidLoad() {
        super.viewDidLoad()

        print("Google Mobile Ads SDK version: " + GADRequest.sdkVersion())
        bannerView.delegate = self
        bannerView.adUnitID = "ca-app-pub-9474695450721030/1823667708"
        bannerView.rootViewController = self
        let req = GADRequest()
        req.testDevices = ["91fbd46dff1179ce0a5e7226cea1ee0b", kGADSimulatorID]
        req.tag(forChildDirectedTreatment: true)
        bannerView.load(GADRequest())
        view.addSubview(bannerView)
        showBanner()
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()

        if let skView = self.view as? SKView {
            if skView.scene == nil {

                let aspectRatio = view.bounds.size.height / view.bounds.size.width
                let scene = MenuScene(size: CGSize(width: 750, height: 750 * aspectRatio))

                scene.scaleMode = .aspectFill
                skView.ignoresSiblingOrder = true

                if kDebug {
                    skView.showsFPS = true
                    skView.showsDrawCount = true
                    skView.showsNodeCount = true
                    skView.showsPhysics = true
                }

                let transition = SKTransition.fade(with: SKColor.black, duration: 0.5)

                skView.presentScene(scene, transition: transition)
            }
        }
    }

    func showBanner() {
        bannerView.isHidden = false
        let request = GADRequest()
        request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
        bannerView.load(request)
    }

    func hideBanner() {
        bannerView.isHidden = true
    }
}

Solution

  • First of all, you don't need to request a new banner every time you run showAd(). Simply unhide it. The ad you load on launch will remain for the whole lifecycle, and automatically refreshes.

    Second, in your viewDidLoad code, you're initializing a banner ad, then loading a completely new one. Make sure that you're loading the same request that you were setting up.

    As for calling functions inside your GameViewController from an SKScene, take a look at this: Call GameViewController function from SKScene