Thursday, 16 April 2015

Integrating with iAd

This is pretty simple... Firstly go to iTunes and then to the Agreements, Tax and Banking section. Select and agree to the iAd App Network contract. Once you have done that, go ahead and open your Xcode project. Go to your project, and in the General section go down to Linked Frameworks and Libraries, hit the '+'and find iAd.framework. The iAd framework comes installed so you don't even need to go and download it first!

iAd BannerView

Go to your view controller where you want to display ads, import iAd and set self.canDisplayBannerAds to true. This will automatically handle the adding of a banner view to your view and resize your view depending on the availability of ads. It will also hide the banner view when no ads can be fetched.

iAd Interstitials

This is a little bit more complicated than banner views but works well when you don't want to make space for banner views, and are commonly used in games. Once again, go to the view controller you want to have show interstitial views and import iAd. You will also need to make it implement ADInterstitialAdDelegate, and add the following code:


    var interstitialAd : ADInterstitialAd!
    var interstitialAdView : UIView!
    var closeButton : UIButton!
    
    override func viewDidLoad() 
    {
        super.viewDidLoad()

        // any viewDidLoad logic you have
        
        closeButton = UIButton.buttonWithType(UIButtonType.System)
                      as! UIButton
        closeButton.frame = CGRectMake(self.view.frame.width - 42,
                                       10, 32, 32)
        closeButton.layer.cornerRadius = 16

        // Note: I have an image asset named "Close"
        // You will need to add one for this to work
        var closeImage = UIImage(named: "Close")
        closeButton.backgroundColor = UIColor.whiteColor()
        closeButton.tintColor = UIColor.blackColor()
        closeButton.setImage(closeImage, 
                             forState: UIControlState.Normal)
        closeButton.addTarget(self, action: "closeAd:", 
                              forControlEvents: .TouchDown)
    }
    
    func closeAd(sender: UIButton) 
    {
        closeButton.removeFromSuperview()
        if ((interstitialAdView) != nil) {
            interstitialAdView.removeFromSuperview()
        }
    }
    
    // call whenever you want to show an ad
    func showAd() 
    {
        interstitialAd = ADInterstitialAd()
        interstitialAd.delegate = self
    }
    
    func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) 
    {
        interstitialAdView = UIView()
        interstitialAdView.frame = self.view.bounds
        self.view.addSubview(interstitialAdView)
        
        interstitialAd.presentInView(interstitialAdView)
        UIViewController.prepareInterstitialAds()
        
        interstitialAdView.addSubview(closeButton)
    }
    
    func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!)
    {
        closeAd(closeButton)
    }
    
    // can do things with the error if you want to... I chose not to
    func interstitialAd(interstitialAd: ADInterstitialAd!,
                        didFailWithError error: NSError!) 
    {
        closeAd(closeButton)
    }




Alternatively, it may be worthwhile implementing this in another view controller that all your view controllers that show interstitial ads will extend.