Search code examples
iosswiftsprite-kitiadskscene

Sprite Kit SKScene with wrong size after integrating iAd or not working at all


I am trying to integrate iAd to a Sprite Kit game.

When I set "canDisplayBannerAds = true" BEFORE adding the game scene, the banner add is shown but the gameScene does not work:

override func viewDidLoad() {
    super.viewDidLoad()

    canDisplayBannerAds = true

    // Configure de main view
    if let skView = view as? SKView {

        // Create and configure scene
        scene = GameScene(size: skView.bounds.size)
        scene!.scaleMode = .AspectFill

        // Show the scene
        skView.presentScene(scene!)
    }
}

Result when setting canDisplayBannerAds = true before adding SKScene

When I set "canDisplayBannerAds = true" AFTER adding the SKScene, the iAd is shown and the game scene is working now, BUT with a wrong size, bigger than the screen left after the banner is added.

override func viewDidLoad() {
    super.viewDidLoad()

    // Configure de main view
    if let skView = view as? SKView {
        skView.showsFPS = true

        // Create and configure scene
        scene = MarketGameScene(size: skView.bounds.size)
        scene!.scaleMode = .AspectFill
        scene!.marketGameViewController = self

        // Show the scene
        skView.presentScene(scene!)
    }

    canDisplayBannerAds = NSUserDefaults.standardUserDefaults().boolForKey(UserDefaultsKey.ShowAds)
}

What am I missing? thanks


Solution

  • Here is some working code for you that just worked for me. This does not even need self.candisplaybannerads = true as I had some issues with that. The ad automatically changes the size according to the screen size and is located at the bottom of the screen. To hide and display the ads use

    AdBanner.hidden = true/false
    

    ......

    import iAd
    
    class viewController: UIViewController, ADBannerViewDelegate {
    
    var AdBanner = ADBannerView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
            /* Ad Banner Settings */
    
            AdBanner = ADBannerView()
            AdBanner.frame = CGRectZero
            AdBanner.delegate = self
            self.AdBanner.frame = CGRectMake(0, self.view.frame.size.height-self.AdBanner.frame.size.height, self.AdBanner.frame.size.width, self.AdBanner.frame.size.height)
            AdBanner.backgroundColor = UIColor.clearColor()
            self.view .addSubview(AdBanner)
    
    }
    
    /* All iAd Functions */
    
    func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
    /* whatever you need */
        return true
    }
    
    func bannerViewActionDidFinish(banner: ADBannerView!) {
    /* whatever you need */
    }
    
    func bannerViewDidLoadAd(banner: ADBannerView!) {
        AdBanner.hidden = false
    }
    
    
    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        NSLog("Error Loading Ad")
    /* whatever you need */
        AdBanner.hidden = true
    }
    func bannerViewWillLoadAd(banner: ADBannerView!) {
    /* whatever you need */
    }