Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

ios - Swift Admob Interstitial Error

This is pretty long but it's probably a really easy fix. Most of this is code, just skip to the bottom and there'll be a tl;dr

Hey, so I've followed about 10 tutorials on getting Admob interstitials to work in my Swift Spritekit game. Except all the tutorials I've seen use an IBAction with a button. Currently I would like the ads to pop up every once in a while on the menu. (just to begin with, I might change it later). This all works perfectly, but once I hit the play button (going to a different scene) and then return to the menu scene. (GameScene) The interstitial function no longer happens. There is no error, it just doesn't do it any more until the app is completely closed and re-opened.

Here's the code.

In my GameViewController class I have:

var interstital: GADInterstitial!

And in the GameViewController did load I have:

if let scene = GameScene(fileNamed:"GameScene") {
        let skView = self.view as? SKView
        skView!.ignoresSiblingOrder = false
        scene.scaleMode = .AspectFill

        scene.viewController = self
        skView!.presentScene(scene)

        interstital = GADInterstitial(adUnitID: "ca-app-pub-9776687746813194/9687097060")

        let request = GADRequest()
        interstital.loadRequest(request)

Also in the GameViewController class I have these functions:

func ShowAd() {
    print("doing the showadfunc now")

    if (interstital.isReady) {

        print("there is an inter ready")

        interstital.presentFromRootViewController(self)
        interstital = CreateAd()

    }
    else{
        print("The interstitial wasn't ready.")
    }
}

func CreateAd() -> GADInterstitial {

    let interstital = GADInterstitial(adUnitID: "ca-app-pub-9776687748683194/9687247060")
    interstital.loadRequest(GADRequest())
    return interstital

}

In my GameScene's (The menu) class I have:

var viewController: GameViewController!

And in my GameScene's class I have this function for displaying an interstitial ad.

func adThing(){
    //viewController?.ShowAd()

    print("starting the ad function")
    let waitForInterstitial = SKAction.waitForDuration(15.0)
    let waitForInterstitialShort = SKAction.waitForDuration(3.0)
    let showInterstitialAd = SKAction.runBlock({self.viewController?.ShowAd(); print("the function has been called..")})
    let waitThenShowInterstitial = SKAction.sequence([showInterstitialAd,waitForInterstitial])
    let waitThenShowInterStitialForever = SKAction.repeatActionForever(waitThenShowInterstitial)
    //self.runAction(waitThenShowInterStitialForever)
    self.runAction(waitForInterstitialShort, completion: {
        print("its waited 3 seconds and will now start the ad thingo")
        self.runAction(waitThenShowInterStitialForever)
    })
    print("done")

}

So, I call the ShowAd function (which is located in gameviewcontroller) in my GameScene every 20 seconds, (I have confirmed it at least thinks it is calling the function.) It works perfectly when the app is opened to begin with. But when I change scene and then return to the menu scene (GameScene), the Prints in my GameScene function keep happening, and my code thinks that it is calling the ShowAd function, but it seems that nothing is happening, no interstitials pop up, not even the prints that are in the ShowAd function in the GameViewController.

So it seems like after I change scenes and return, my game forgets what the GameViewController is. If I refer to the GameViewController without a '?' it will crash my game with the error "unexpectedly found nil while unwrapping an optional." So after changing scenes, maybe GameViewController becomes nil? How can I fix this.

Please help me!!! (I'm new to programming in case you can't tell. I know it must be tempting to make a snide comment about my code, it is really bare bones and not that good at the moment.)

Thank you so much if you can help me, and wow.. thanks for reading all this.. ;)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'd bet this is your issue:

if (interstital.isReady) {
    print("there is an inter ready")
    interstital.presentFromRootViewController(self)
    interstital = CreateAd()

You're presenting your interstitial and then immediately overwriting your interstitial. You should be implementing the GADInterstitial's delegate methods and calling your func CreateAd() -> GADInterstitial once func interstitialDidDismissScreen(ad: GADInterstitial!) is called.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...