Search code examples
iosswiftiad

Swift SpriteKit iAd


I'm developing a game in Swift and I have a little problem that I couldn't solve. I'm working only with scenes, I have no UIViews. The main scene is where the game runs and when the players dies a new scene will be loaded. I want in that scene, where the player dies, to display a menu (which I did) and to display iAd banners. I tried also with UIViewControllers but I couldn't manage it. I want to make it only in SpriteKit and I don't know how. Could anybody help me please?


Solution

  • If you simply want to display an iAd banner then you'll need to do several things. First, import the iAd framework and import iAd the the top of your code. Then, use this function to display the banner. (oh, and adBannerView should be declared as a global variables within your skscene).

    func loadAds()->ADBannerView{
        adBannerView = ADBannerView(frame: CGRect.zeroRect)
        adBannerView.center = CGPoint(x: adBannerView.center.x, y: view!.frame.size.height - adBannerView.frame.size.height / 2)
        adBannerView.delegate = self
        self.view?.addSubview(adBannerView)
        return adBannerView
    }
    

    You may also want to include these functions. This one right here runs when the banner cannot load (this will most likely occur due to a network problem).

    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        println("Ad cannot load")
        self.adBannerView.hidden = true
    }
    

    This runs when the banner successfully loads.

    func bannerViewDidLoadAd(banner: ADBannerView!) {
        println("ad did load.")
        self.adBannerView.hidden = false
    }