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

怎甘沉沦 提交于 2019-12-04 10:27:42

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.

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.

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