noob question: how do i add a shared adbanner in a swift sprite kit game

Hi guys,


Im struggling to get my code working, i want to add a shared iads adbanner in the appdelegate class and turn it on and off in seperate swift file scenes. Im new to this so excuse my ignorance! here is my appdelegate code:


I get the appropriate 'printed' lines but the adbanner never pops up...


i call this in my 'GameScene' file:


NSNotificationCenter.defaultCenter().postNotificationName("showiAdBanner", object: nil)


AppDelegate file: (Ignore the size of the banner... i havent worked that bit out yet!)

import UIKit
import iAd
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, ADBannerViewDelegate {
    var window: UIWindow?
    var bannerAd:ADBannerView = ADBannerView(frame: CGRect(x: 0, y: 0, width: 1024, height: 100))

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        /
     
        /
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "showiAdBanner", name: "showiAdBanner", object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "hideiAdBanner", name: "hideiAdBanner", object: nil)
        bannerAd.delegate = self
        return true
    }
    func applicationWillResignActive(application: UIApplication) {
        /
        /
    }
    func applicationDidEnterBackground(application: UIApplication) {
        /
        /
    }
    func applicationWillEnterForeground(application: UIApplication) {
        /
    }
    func applicationDidBecomeActive(application: UIApplication) {
        /
    }
    func applicationWillTerminate(application: UIApplication) {
        /
    }
    func showiAdBanner() {
     
       /
            bannerAd.hidden = false
        /
        print("showiadbanner")
    }

    func hideiAdBanner() {
        bannerAd.hidden = true
        print("hideiadbanner")
    }

    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        bannerAd.hidden = true
        print("bannerViewError")
    }

    func bannerViewWillLoadAd(banner: ADBannerView!) {
        print("bannerViewWillLoadAd")
    }

    func bannerViewDidLoadAd(banner: ADBannerView!) {
        print("bannerViewDidLoadAd")
        bannerAd.hidden = false
            }

    func bannerViewActionDidFinish(banner: ADBannerView!) {
        print("bannerViewActionDidFinish")
    }

    func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
        print("bannerViewActionShouldBegin")
        return true
    }



}

Banners are presented from your ViewController, not AppDelegate. The easiest way to make this happen is via the canDisplayBannerAds method, called from your ViewController's viewDidLoad.


See https://developer.apple.com/library/ios/documentation/iAd/Reference/UIViewController_iAd_Additions/index.html#//apple_ref/occ/instp/UIViewController/canDisplayBannerAds for more details.


-Josh

Thanks josh, I want to be able to switch it on only in the game and game over scenes though? Not have it on constantly. Is that possible? Can I use code similar to the above? I understand it's difficult on a forum but any help would be massively appreciated. I think I tried using the view controller first time around but I had issues with it being a SpriteKit UIView (I'll try again that way and let you know what the issue is)

OK, i tried that and got this error:


Could not cast value of type 'UIView' (0x1089c0578) to 'SKView' (0x107c2da18).

(lldb)


This is the code:


import UIKit
import SpriteKit
import AVFoundation
import iAd
class GameViewController: UIViewController, ADBannerViewDelegate  {
  
    var backgroundMusicPlayer:AVAudioPlayer=AVAudioPlayer()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.canDisplayBannerAds = true
    }
  
  
    override func viewWillLayoutSubviews() {
      
        let bgMusicURL:NSURL = NSBundle.mainBundle().URLForResource("bgmusic", withExtension: "mp3")!
        try! backgroundMusicPlayer  = AVAudioPlayer(contentsOfURL: bgMusicURL) as AVAudioPlayer
      
        backgroundMusicPlayer.numberOfLoops = -1
        backgroundMusicPlayer.prepareToPlay()
        backgroundMusicPlayer.play()
      
        var skView:SKView = self.view as! SKView
        skView.showsFPS = false
        skView.showsNodeCount = false
        var scene:SKScene = GameTitles(size: skView.bounds.size)
        scene.scaleMode = SKSceneScaleMode.AspectFill
        skView.presentScene(scene)
    }
  
    override func shouldAutorotate() -> Bool {
        return true
    }
  
    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
            return .AllButUpsideDown
        } else {
            return .All
        }
    }
  
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        /
    }
  
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
}

You likely have a line of code like this in your GameViewController


let skView = self.view as! SKView


For Automatic presentation (canDisplayBannerAds) to work, try leaving the default ViewController view as is and adding a second view (SKView) for your game to run in. So we would modify the above to after (1) adding the UIView in the Storyboard (and setting appropriate constraints) and (2) adding a reference to the view in your GameViewController.swift file.


let skView = self.gameView as! SKView


Depending on when you would like to present the banner in your game play, you may need to manage presentation of the banner manually.

Thanks Josh, I'll give this a go. Sorry about the new thread I thought it looked like seperate issue.

Did this work for you?

Hi Josh,


Sorry about the delay i have been holidaying in the big apple! Im back now though and am intending to try this tomorrow evening. It does look rather complicated and i dont fully understand what you mean as im not sure on what the storyboard does and what each of the components do yet.. ill amend the code from


var skView:SKView = self.view as! SKView
skView.showsFPS = false
skView.showsNodeCount = false
var scene:SKScene = GameTitles(size: skView.bounds.size)  scene.scaleMode = SKSceneScaleMode.AspectFill


to:


var skView:SKView = self.gameView as! SKView
skView.showsFPS = false
skView.showsNodeCount = false
var scene:SKScene = GameTitles(size: skView.bounds.size)  scene.scaleMode = SKSceneScaleMode.AspectFill

and see if that does the trick? As i said I havent used the storyboard so unsure what to do there, i havent studied about that yet i went straight into code as its what i know but if you have an explaination of what it is id be glad to hear it! I have a huge passion but no experience and no one to learn from so its difficult and frustrating as He|| but i know us noobs are frustrating for you experts too!


thanks for your help

ive had a go at this and im lost. I have added a second view controller on the main storyboard but i have no idea what to do now? To be honest i dont know what we are trying to achieve.


I dont know how to name it or then how to use it to achieve what i need,

Ok,


I did some learning and understand a little more now. I left the storyboard as i need to get to know that and attacked this with code. I managed to create a new UIView and popped that into the GameViewController in order to run the game. I ran it initially without setting is as the current view and the default view showed grey background and the iads so we are on the right track. I then told the controllwer to set its view to the new one which ran the game.


My problem is that the new view is infront of the old one which is showing the ad. do i have to layer them or use the adbanner in the new view?

hi

This is what I did:

In GameViewController, this line in my " override func viewDidLoad() {" :

self.canDisplayBannerAds = true


and that's it.


If you need to know when the banner is on or off just like I needed, I use the following in GameViewController:



override func viewDidLayoutSubviews() {

super.viewDidLayoutSubviews()

if self.displayingBannerAd == false {

adBottom = true

} else {

adBottom = true

}

}


a bit of a strange code! but it works! You can check it for your self from the following app on the store: https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=1028900746&mt=8


by the way, as from level 4, I have an interstitial ad presented from a sks file. If you need the code, let me know.

This is what i did (See my GameViewController code above) But it still doesnt show?


Could it be todo with this


        var scene:SKScene = GameTitles(size: skView.bounds.size) 
        scene.scaleMode = SKSceneScaleMode.AspectFill
noob question: how do i add a shared adbanner in a swift sprite kit game
 
 
Q