简体   繁体   中英

How can I create a completion handler with admob interstital ads in Swift?

I'm trying to pause the background of my app (which is a game) when admob's interstitial ad is running. When the user decides to close the ad, then the game should resume. This would work best with a completion handler but GoogleMobileAds doesn't seem to offer one. Is there way to make one?

What the code would look like:

func gameDidEnd(relish: Relish) {
        view.userInteractionEnabled = false
        scene.stopTicking()

        let score: Int = Int(scoreLabel.text!)!

        if score > userHS {
            updateHighScore(score)
        }

        if (interstitial.isReady) {
            interstitial.presentFromRootViewController(self)
            interstitial = createAd()

            if interstitial.pressedCloseOnAd {
               scene.animateCollapsingLines(relish.removeAllBlocks(), fallenBlocks:
               relish.removeAllBlocks()) {
                relish.beginGame()
                getCurrentReward(user.ID)
            }
        }

        else {
            scene.animateCollapsingLines(relish.removeAllBlocks(), fallenBlocks:
            relish.removeAllBlocks()) {
            relish.beginGame()
            getCurrentReward(user.ID)
        }


    }

Also I'm running into the issue where I try to display an interstitial ad after they exit the game page with tappedClose . Originally, when tappedClose was called, dismissViewControllerAnimated would be called. If I try and run an interstitial ad before that, dismissViewControllerAnimated would just close out the ad. If I put the interstitial ad after dismissViewControllerAnimated , the ad never runs (because it is not in the window hierarchy).

Thanks for your help!


Updated

func interstitialWillPresentScreen(ad: GADInterstitial!) {
    view.userInteractionEnabled = false
    scene.stopTicking()
    print("interstitialWillPresentScreen")
}

func interstitialDidDismissScreen(ad: GADInterstitial!, relish: Relish) {
    view.userInteractionEnabled = true
    scene.startTicking()
    getCurrentReward(user.relationshipID)
    print("interstitialDidDismissScreen")
}

func gameDidEnd(relish: Relish) {
    view.userInteractionEnabled = false
    scene.stopTicking()

    let score: Int = Int(scoreLabel.text!)!

    if score > userHS {
        updateHighScore(score)
    }

    if (interstitial.isReady == false) {
        print("is not ready")
        scene.animateCollapsingLines(relish.removeAllBlocks(), fallenBlocks:
        relish.removeAllBlocks()) {
            relish.beginGame()
        }

        getCurrentReward(user.relationshipID)
    }

    else if (interstitial.isReady) {
        print("is ready")
        interstitial.presentFromRootViewController(self)
        interstitialWillPresentScreen(interstitial)
        interstitialDidDismissScreen(interstitial, relish: relish)
        interstitial = createAd()
    }
}

When the game ends, the console reads:

is ready
interstitialDidDismissScreen
2016-09-06 22:22:43.915 file[5321:347179] <Google> Request Error: Will not send request because interstitial object has been used.
is not ready

The last is not ready signifies that the game continued in the background even though when gameDidEnd was called, interstitial.isReady was true so it should've only ran the else if statement.

You can make your code work this way I think:

1.Set your interstial ad's delegate to your class

2.Stop the game when you are presenting the interstial ad by this delegate method:

func interstitialWillPresentScreen(ad: GADInterstitial!) {
    // Stop the game
}

3.Resume your game after user closes the interstial ad with this delegate method:

func interstitialDidDismissScreen(ad: GADInterstitial!) {
    // Resume the game
}

There are some other GADInterstitialDelegate methods you might want to check out, if you do refer to this link .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM