Search code examples
xcodeswiftsprite-kitxcode6iad

Implementing iAd in Sprite Kit with Swift


I've been trying to figure out how to implement a portrait banner ad view at the bottom of my Sprite Kit game with Swift and am quite confused as to how to do it.

My references were the iAd Guide from Apple, which is unfortunately in Obj-C(I'm new to the whole programming scene so forgive my ignorance of Obj-C), as well as these: iAd in sprite kit game

iAd in xcode 6 with Swift

http://codewithchris.com/iad-tutorial/

If anyone could suggest a method I would be really grateful to you, thanks for reading :)


Solution

  • Code with AutoLayout(code in viewDidLoad()):

        let bannerAd = ADBannerView(adType: ADAdType.Banner)
        bannerAd.delegate = self
        bannerAd.setTranslatesAutoresizingMaskIntoConstraints(false)
        self.view.addSubview(bannerAd)
        let constraintsH = NSLayoutConstraint.constraintsWithVisualFormat("|[bannerAd]|", options: nil, metrics: nil, views: ["bannerAd":bannerAd])
        let constraintsV = NSLayoutConstraint.constraintsWithVisualFormat("V:[bannerAd(50)]|", options: nil, metrics: nil, views: ["bannerAd":bannerAd])
        self.view.addConstraints(constraintsH)
        self.view.addConstraints(constraintsV)
    

    Don't forget to implement delegate methods.