1

I'm trying to set up iAd Banner in a SpriteKit Game, for this Im using as an example the SpriteKit template provided by Apple, SpaceShip. Using this example as a guide Example Swift iAd The problem I have is that the example provided is for a SpriteKit and thus there are no SkScenes interactions. Thus trying to replicate this in a GameViewController has been imposible for me. Here is the my code.

import UIKit
import SpriteKit
import iAd

extension SKNode {
class func unarchiveFromFile(file : NSString) -> SKNode? {
    if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") {
        var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)!
        var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)

        archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
        let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as GameScene
        archiver.finishDecoding()
        return scene
    } else {
        return nil
    }
}
}

class GameViewController: UIViewController, ADBannerViewDelegate {

var bannerView:ADBannerView?


override func viewDidLoad() {
    super.viewDidLoad()

    self.canDisplayBannerAds = true
    self.bannerView?.delegate = self
    self.bannerView?.hidden = true

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

override func shouldAutorotate() -> Bool {
    return true
}

override func supportedInterfaceOrientations() -> Int {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
    } else {
        return Int(UIInterfaceOrientationMask.All.rawValue)
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Release any cached data, images, etc that aren't in use.
}

override func prefersStatusBarHidden() -> Bool {
    return true
}

func bannerViewDidLoadAd(banner: ADBannerView!) {
    self.bannerView?.hidden = false
}

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

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
    self.bannerView?.hidden = true
}

}

This code runs but hits a BreakPoint that prints no message to the console.

Alternate Code

Reading similar questions I found this Stackoverflow QuestionI tried that and worked better, Game Scene loaded and iAdBanner was loaded and displayed, the problem with this is that the iAd banner is not clickable and thus the add banner does not load to fullscreen. Here is the code I used for this:

import UIKit
import SpriteKit
import iAd


extension SKNode {
    class func unarchiveFromFile(file : NSString) -> SKNode? {
        if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") {
            var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)!
            var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)

            archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
            let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as GameScene
            archiver.finishDecoding()
            return scene
        } else {
            return nil
        }
    }
}

class GameViewController: UIViewController, ADBannerViewDelegate {

    @IBOutlet var adBannerView: ADBannerView?

    override func viewDidLoad() {
        super.viewDidLoad()
        loadAds()


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

    override func shouldAutorotate() -> Bool {
        return true
    }

    override func supportedInterfaceOrientations() -> Int {
        if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
            return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
        } else {
            return Int(UIInterfaceOrientationMask.All.rawValue)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Release any cached data, images, etc that aren't in use.
    }

    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    func bannerViewDidLoadAd(banner: ADBannerView!) {
        self.adBannerView?.hidden = false
    }

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

    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        self.adBannerView?.hidden = true
    }

    func loadAds() {
        adBannerView = ADBannerView(frame: CGRect.zeroRect)
        self.adBannerView?.delegate = self
        self.adBannerView?.hidden = true
        view.addSubview(adBannerView!)

    }
}

Thank you any help would be really appreciated.

Community
  • 1
  • 1
  • If it hits a breakpoint, it's most likely because you've added one. Check the tab on the left-side pane that's called Breakpoint Navigator. And hit the continue button in the debug area at the center bottom to see whether execution continues or an error is logged. As is, this question is too broad to answer. – CodeSmile Nov 09 '14 at 10:11
  • I've checked that I have no breakpoints activated. The breakpoint that activates is within the library files and not within my swift classes. The link that I added to another Stackoverflow question has the same problem (hitting a breakpoint) the solution they offer is basically my alternate code, but still the iAd banner is not clickable. Basically I need help in setting iAd Banner within Sprite Kit or SKScenes. – Emanuel Gonzalez Nov 09 '14 at 22:12

0 Answers0