How to call admist interstitial using fast, spritekit and xcode?

I was looking for answers to all of this, and I found many examples in objective C (Google developer docs, etc.), and some answers are quick but don't use spritekit and are new to I just couldnโ€™t bridge the gaps in these tutorials so bring it all together for my project. Honestly, I donโ€™t care if the ad is triggered when the application is launched or in the game, Iโ€™ll just be happy that you can name the period of advertising, although I assume that the launch of the game is preferable.

I am sure that everything is configured correctly in gameviewcontroller.swift, but I do not know what to 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

    }

... if , , ... ( , , :

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

- ? - , , , ... . .

+4
3

, ( Swift), Game Project

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
    }
}

, , - ยฑ 5 , .

, , , - , โ†’ , AD

! admob ID ID.

+9

, , :

1) 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)             
        }
    }

GameScene

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

, .

+3

save the if condition in viewDidAppear, it will automatically execute

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

Source: https://habr.com/ru/post/1584396/