Google Admob with Sprite-KIt in Swift

672 views
Skip to first unread message

Vinny Amato

unread,
Aug 13, 2015, 5:53:41 PM8/13/15
to Google Mobile Ads SDK Developers

I would like to display Google Ads in my Sprite-Kit game. In the GameViewController I create the banner ad:

override func viewWillLayoutSubviews(){
    super.viewWillLayoutSubviews()

    let skView = self.view as! SKView

    googleBannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)

    googleBannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"

    googleBannerView.rootViewController = self

    var request: GADRequest = GADRequest()
    googleBannerView.loadRequest(request)

    googleBannerView.frame = CGRectMake(0, skView.bounds.height - googleBannerView.frame.size.height, googleBannerView.frame.size.width, googleBannerView.frame.size.height)

    self.view.addSubview(googleBannerView!)

    println(googleBannerView)

    if skView.scene == nil{

        let mainMenuScene = MainMenuScene(size: skView.bounds.size)
        mainMenuScene.scaleMode = SKSceneScaleMode.AspectFill
        mainMenuScene.backgroundColor = UIColor.whiteColor()

        skView.presentScene(mainMenuScene)
    }
}

This works fine except for when I switch to the GameScene. I would like the ad to be hidden. Then in the GameOverScene I would like the add to be shown again. The problem is that the ad is equal to nil for some reason when I switch scenes, and it stays equal to nil. Here are the hideBanner() and showBanner() functions that don't work because it is equal to nil after the game switches scenes.

The hideBanner() function in the GameViewController:

func hideBanner(){
    if googleBannerView != nil{
        println("hideBanner() called")
        self.googleBannerView.hidden = true
    }
}

The showBanner() function in the GameViewController: 

func showBanner(){
    if googleBannerView != nil{
        self.googleBannerView.hidden = false
        var request: GADRequest = GADRequest()
        self.googleBannerView.loadRequest(request)
    }
}

The problem is as soon as it switches scenes the banner view is equal to nil, yet I can still see the banner; it hasn't changed. 

In the the GameScene:

override init(size: CGSize) {
    super.init(size: size)

    let gameViewController = GameViewController()

    println(gameViewController.googleBannerView)

    gameViewController.hideBanner()

And then the console prints out nil. I don't know why when I switch scene the ad banner becomes nil. In the MainMenuScene where the banner is created it prints out:

<GADBannerView: 0x7fe982715050; frame = (0 617; 375 50); clipsToBounds = YES; layer = <CALayer: 0x7fe982400fd0>>

After that it seems to always be equal to nil, even when I come back to the MainMenuScene. I just can't figure it out. Does anyone know what to do? The help is appreciated. Thank you in advance.

-Vinny

Elton Kent

unread,
Aug 14, 2015, 10:46:46 AM8/14/15
to Google Mobile Ads SDK Developers
Hi Vinny,
Can you try prefetching the banner instead? I see that you fire an Ad request in showBanner(). You could, maybe, hide the banner view but fire the ad request. That way, you have a banner available when it's time to show it.

Cheers,
Elton Kent 
Mobile Ads SDK Team

Vinny Amato

unread,
Aug 14, 2015, 11:30:05 AM8/14/15
to google-adm...@googlegroups.com
Hi Elton,
Thank you for responding. As I am pretty new to Swift zi don’t really know how to do this. Would you mind helping me? Thank you in advance.

-Vinny

--

---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/45xA52nsLBM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-admob-ads...@googlegroups.com.
To post to this group, send email to google-adm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vinny Amato

unread,
Aug 14, 2015, 11:30:37 AM8/14/15
to Google Mobile Ads SDK Developers
Hi Elton,
Thank you for responding. As I am pretty new to Swift zi don’t really know how to do this. Would you mind helping me? Thank you in advance.

-Vinny

Elton Kent

unread,
Aug 14, 2015, 7:01:30 PM8/14/15
to Google Mobile Ads SDK Developers
Hi Vinny,
You can find information regarding banner implementations at this page.

Cheers,
Elton Kent
Mobile Ads SDK Team

Reply all
Reply to author
Forward
0 new messages