Categories
iOS SWIFT

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

The easiest way to add iAD banners 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 way 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 to integrate iAD.

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

1. Create the sample project:

 

iAd Tutorial iAd Tutorial

2. Ad the iAD framework to your project:

iAd Tutorial iAd Tutorial iAd Tutorial

 

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. Integrate iAd iAd Tutorial

That’s all for today.

Cheers,
Stefan