How to call admob interstitial ad using swift, spritekit and xcode?

一曲冷凌霜 提交于 2019-12-21 19:59:13

问题


I've been looking all over for an answer to this and I've found lots of examples in objective C (google developer docs, etc.) and some answers in swift, but not using spritekit, and being a novice, I just haven't been able to bridge the gaps in these tutorials to put it all together for my project. Honestly, I don't really care if the ad is called on app launch, or game over, I'd just be happy being able to call the ad period, though I guess on game launch is preferred.

Pretty sure I've got it all set up correctly in gameviewcontroller.swift, but I don't know how to actually call it. Here is the code from my gameviewcontroller.swift:

import UIKit
import SpriteKit
import GoogleMobileAds

class GameViewController: UIViewController {

    var interstitial : GADInterstitial!

 func createAndLoadAd() -> GADInterstitial {

        var ad = GADInterstitial()
        ad.adUnitID = "ca-app-pub-4471013071748494/2980967718"
        var request = GADRequest()
        request.testDevices = [""]

        return ad

    }

Beyond this ... I also have this if statement to call the ad, but I don't know where it should be ... or if it's complete (in the tutorial, it was in a button, but I need it to be automatic, naturally:

if (self.interstitial.isReady) {
            self.interstitial.presentFromRootViewController(self)
            self.interstitial = self.createAndLoadAd()
        }

Anyone have any experience with this? If anyone could help me complete this by letting me know where to place that above if statement, or if there is more that needs to be done ... your help will be much appreciated. Thank you.


回答1:


Ok so I started a new project (Swift project) which is Game Project

In GameViewController:

class GameViewController: UIViewController
{
var interstitial = GADInterstitial()

override func viewDidLoad()
{
    super.viewDidLoad()

    if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene
    {
        // Configure the view.
        let skView = self.view as! SKView
        skView.showsFPS = true
        skView.showsNodeCount = true

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill

        skView.presentScene(scene)



        let delayTime = dispatch_time(DISPATCH_TIME_NOW,
            Int64(5 * Double(NSEC_PER_SEC)))

        //Creation of the ad and its request
        self.interstitial = GADInterstitial()
        self.interstitial.adUnitID = "ca-app-pub-4798156420380453/7714267723"
        var request = GADRequest()
        // request.testDevices = [""]
        self.interstitial.loadRequest(GADRequest());//The method u were missing

        dispatch_after(delayTime, dispatch_get_main_queue())
        {
            self.showAd()
        }

    }
}



 func showAd()
 {
    if (self.interstitial.isReady)
    {
        self.interstitial.presentFromRootViewController(self)//Whatever  shows the ad
    }
}

It is my first swift code so don't judge me if I did something wrong But when I lunch it after ±5 seconds i see the ad Here is your example.

I am not familiar with Swift at all , but all you need to do is to get the interstitial delegate when it dismissed -> and request for the next interstitial, So in next time you will get the new AD

Feel free to ask anything ! P.S. Don't forget to change the admob ID to your ID.




回答2:


My solution based on above comments was the following:

1) Add this in the GameViewController.swift

protocol AdmobInterstitialDelegate{
    func showInterstitial()
}
...
// add this delegate in GameViewController definition
class GameViewController: UIViewController, GADInterstitialDelegate, AdmobInterstitialDelegate {
...
// and assign it to the GameScene class when you create its instance
     let scene = GameScene()
     scene.adDelegate = self
// do not forget to implement the delegated method itself
    func showInterstitial(){
        if (interstitial!.isReady) {
            interstitial!.presentFromRootViewController(self)             
        }
    }

and then you can use it in GameScene

class GameScene: SKScene {
....
    var adDelegate: AdmobInterstitialDelegate?
....
// and finally - show the ad
    override func didMoveToView(view: SKView) {
        self.adDelegate?.showInterstitial();
        ....

hope this helps.




回答3:


keep the if condition in viewDidAppear it will automatically execute

override func viewDidAppear(animated: Bool) {
        if (interstitial.isReady) {
        interstitial.presentFromRootViewController(self)
        self.interstitial = self.createAndLoadInterstitial()
        }


来源:https://stackoverflow.com/questions/29838262/how-to-call-admob-interstitial-ad-using-swift-spritekit-and-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!