iosswiftiadscenekit

How do I enable ads in a Swift SceneKit game when it's game over?


If you have ever played Flappy Bird and get a game over, an ad gets displayed on the top of the app until your start a new game. I want to do that, but I'm having a hard time passing the required code into the gameOver function (if possible somehow).

Do I have to add a whole new scene for this or can I just do it in the class of the GameScene?

I know to display ads, you go to the ViewController.swift and do this:

class GameViewController: UIViewController {

  override func viewDidLoad() {
   super.viewDidLoad()
   let scene = GameScene(size: CGSize(width: 2048, height: 1536))
   let skView = view.self as! SKView
   skView.ignoresSiblingOrder = false
   scene.scaleMode = .AspectFill
   skView.presentScene(scene)

   self.canDisplayBannerAds = true
  }
}

The only problem with this is that it will display ads the entire time, even when the game is running. As soon as it moves in to the GameScene.swift file, the ads are still running. So, I tried to add the viewDidLoad() into the GameScene.swift file (which is the whole game screen. My game has no title scene or gameover scene. Those scenes take place in the GameScene class; all it does is show an image).

I added this into the GameScene.swift but it doesn't do anything.

class adBannerView: UIViewController {

 override func viewDidLoad() {

   super.viewDidLoad()

   self.canDisplayBannerAds = true

  }
}

How do I make ads pop up when the gameOver() function ocurs? Or when it's game over? I've tried looking at other questions but they were different than mine. I can't just add self.canDisplayAds = true inside the gameOver function.

Note that code is required is Swift 2.0 and I'm using Xcode 7 Prerelease 2.


Solution

  • SOLUTION I solved it while looking around a bit more. I added the following code to my viewController class.

    var SH = UIScreen.mainScreen().bounds.height
    let transition = SKTransition.fadeWithDuration(0)
    var UIiAd: ADBannerView = ADBannerView()
    
    override func viewWillAppear(animated: Bool) {
        var BV = UIiAd.bounds.height
        UIiAd.delegate = self
        UIiAd.frame = CGRectMake(UIScreen.mainScreen().bounds.width / 4.6, SH + BV, 0, 0)
        self.view.addSubview(UIiAd)
    }
    
    override func viewWillDisappear(animated: Bool) {
        UIiAd.delegate = nil
        UIiAd.removeFromSuperview()
    }
    
    func bannerViewDidLoadAd(banner: ADBannerView!) {
        var BV = UIiAd.bounds.height
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(0.5) // Time it takes the animation to complete
        UIiAd.alpha = 1 // Fade in the animation
        UIView.commitAnimations()
    }
    
    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(0.5)
        UIiAd.alpha = 0
        UIView.commitAnimations()
    }
    
    func showBannerAd() {
        UIiAd.hidden = false
        var BV = UIiAd.bounds.height
    
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(0.5) // Time it takes the animation to complete
        UIiAd.frame = CGRectMake(UIScreen.mainScreen().bounds.width /  4.6, SH - BV, 0, 0) // End position of the animation
        UIView.commitAnimations()
    }
    
    func hideBannerAd() {
        UIiAd.hidden = true
        var BV = UIiAd.bounds.height
    
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(0.5) // Time it takes the animation to complete
        UIiAd.frame = CGRectMake(UIScreen.mainScreen().bounds.width /  4.6, SH + BV, 0, 0) // End position of the animation
        UIView.commitAnimations()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.UIiAd.hidden = true
        self.UIiAd.alpha = 0
    
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "hideBannerAd", name: "hideadsID", object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "showBannerAd", name: "showadsID", object: nil)
    //My other code
    }
    

    I then added this code into the scene.swift file that will show the ads. These functions are placed outside the class.

    func showAds() {
        NSNotificationCenter.defaultCenter().postNotificationName("showadsID", object: nil)
    }
    func hideAds() {
    NSNotificationCenter.defaultCenter().postNotificationName("hideadsID", object: nil)
    }
    

    And then when I want to show ads, I call the function in the gameOver() with showAds(). Works perfect!!