How do I put advertisements into my game using iAd?

| | August 10, 2015

I’ve made a game using Spritebuilder and is written in Swift and want to add advertisements before I ship an update to the app store. I want help as to how to do this. Any help is appreciated.

2 Responses to “How do I put advertisements into my game using iAd?”

  1. It’s fairly easy.

    1) Import the iAd framework >Build Phases> Link Binary With Libraries> find iAd

    2) drag an iAd BannerView from the object library

    3) Make an outlet connecting the iAd BannerView to your VC file

    4) Add the delegate to your VC class

    import iAd
    
    class ViewController: UIViewController, ADBannerViewDelegate{
    
    @IBOutlet var adBannerView: ADBannerView?
    
    override func viewDidLoad(){
    
        self.canDisplayBannerAds = true
        self.ADBannerView?.delegate = self
        self.ADBannerView.hidden = true
    
    }
    
    func bannerViewWillLoadAd(banner: AdBannerView!){ // any addition set up you want
    }
    
    func bannerViewDidLoadAd(banner: ADBannerView!){
        self.ADBannerView?.hidden = false
    }
    
    func bannerViewActionDidFinish(banner: ADBannerView!){// any addition set up you want
    }
    
    func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Boll) -> Bool {
        return true
    }
    
    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!){
        self.adBannerView?.hidden = false
    }
    
    }
    

    Of course you’ll have to adjust to display when ever and where you wan it.

  2. To add iAd to your game, you need to add the iAd framework to your app (via Build Phases) and add the import statement import iAd in the class that handles your iAd implementation.

    The simplest iAd ad to add to your Swift game is a banner ad:

    override func viewDidLoad() {
    super.viewDidLoad()
    self.canDisplayBannerAds = true
    }
    

    Make sure to import iAd and add the AdBannerViewDelegate protocol to your class declaration:

    import iAd
    class MasterViewController: UITableViewController, NSFetchedResultsControllerDelegate,ADBannerViewDelegate
    

    Then you need to create the actual banner view, set the delegate (in ViewDidLoad) and add two delegate methods, so that you know when to add/remove the advertisement banner from the view:

    bannerViewDidLoadAd:
    bannerView:didFailToReceiveAdWithError:
    

Leave a Reply