2

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
    }
}
Mike
  • 1,307
  • 3
  • 17
  • 29
  • You don't need to request a new banner every time you run showAd(). Simply unhide it. Also, in your viewDidLoad code, you're initializing a banner ad, then loading a different one – Nik Dec 05 '16 at 20:03
  • @Nik In regards to calling show/hideBanner(), how do I access these funcs (and the bannerView itself) from the other scene files if they're local to the vc file here? Gahh, I would do that! Funny, I remember looking at that line and telling myself to change that as I was setting the attributes of the req variable. Cheers! – Mike Dec 05 '16 at 20:10
  • Take a look at this for calling vc functions from scenes: http://stackoverflow.com/a/40409261/6728196 – Nik Dec 05 '16 at 20:12
  • 1
    Thank you! If you'd like to add your comment as an answer, I'd be happy to mark it. – Mike Dec 05 '16 at 20:12

2 Answers2

3

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

Community
  • 1
  • 1
Nik
  • 1,664
  • 2
  • 14
  • 27
0

Just in case anyone is not seeing the ads after updating the SDK. From my end, I was able to resolve it after much investigation. It is worth noticing that the function name changes after updating the SDK.

The old function is

func adViewDidReceiveAd(_ bannerView: GADBannerView) 

The new function is

func bannerViewDidReceiveAd(_ bannerView: GADBannerView)
Kelvin Tan
  • 327
  • 3
  • 15