Pages

Wednesday, July 8, 2015

Quick Tip: Integrate iAd banners in your iOS App with two lines of code

The easiest way to add an iAD banner to your app


Today I'll show a very simple way to integrate an ad banner to your iOS app. Most tutorials (including mine) explain the complex version by implementing the ADBannerViewDelegate protocol. If you don't care about error handling, animations, positioning or the maximum number of allowed banner instances (10!): There is a much easier way which requires only two lines of code in SWIFT.

For details how to subscribe for the Apple iAds program, please check my previous article about iAD integration.

1. Create the sample project:



2. Ad the iAD framework to your project:






3. Add these two lines of code to your ViewController class:


import UIKit
import iAd

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.canDisplayBannerAds = true
    }


}


Thats all! Apple takes care about ads loading and error handling and decides, if the ad is ready to show. Apple also handles orientation changes and the different sizes for iPhone and iPad.



That's all for today.

Cheers,
Stefan

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete