SpriteKit IOS support with Swift

I followed the Documentation I found (googleAd in swift). when I performed in my stage SpriteKit

. The command line " bannerView? .rootViewController = Self

" error appears.

gamescene not convertible to UIViewController

 bannerView = GADBannerView(adSize: kGADAdSizeBanner)
            bannerView?.adUnitID = "xxxxxxxxxxxxxxxxxxxxx"
            bannerView?.delegate = self
            bannerView?.rootViewController = self // -> Error "gamescene not convertible to UIViewController"
            self.view?.addSubview(bannerView!)
            bannerView?.loadRequest(GADRequest())

            timer?.invalidate()
            timer = NSTimer.scheduledTimerWithTimeInterval(40, target: self, selector: "GoogleAdRequestTimer", userInfo: nil, repeats: true)

      

+3


source to share


1 answer


You need to put this code in viewDidLoad in GameViewController.swift not in GameScene.swift.

Don't forget to add the GADBannerViewDelegate to the class.

Example:

class GameViewController: UIViewController, GADBannerViewDelegate {

var scene: GameScene!
var adBannerView: GADBannerView!

override func viewDidLoad() {
    super.viewDidLoad()

        // Configure the view.
        let skView = view as SKView
        skView.showsFPS = false
        skView.showsNodeCount = false
        skView.showsPhysics = false

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

        /* Set the scale mode to scale to fit the window */
        scene = GameScene(size: skView.bounds.size)
        scene.scaleMode = .AspectFill

        skView.presentScene(scene)

    adBannerView = GADBannerView(frame: CGRectMake(0, 0, self.view.frame.size.width, 50))
    adBannerView.delegate = self
    adBannerView.rootViewController = self
    adBannerView.adUnitID = "YOUR AD ID"

    var reqAd = GADRequest()
    reqAd.testDevices = [GAD_SIMULATOR_ID] // If you want test ad's
    adBannerView.loadRequest(reqAd)
    self.view.addSubview(adBannerView)


}

      



I am linking to two links that provide examples of including Google Ad (AdMob) in an application.

Google Ad with Swift Example 1 (in more detail)

Google Ad with Swift Example 2

I hope this helps

+4


source







All Articles