Tutorial – Adding a shared iAd Banner using App Delegate
In this post we will look at adding a shared iAd banner using the App Delegate. The reason for adding a shared iAd banner is simple – as you move to different view controllers in the application the iAd only needs to load once, where if it isn’t shared each view controller has to load the iAd separately – resulting in a worse off user expereince. The differences are illustrated below:
Not using a shared ad banner
Using a shared ad banner
More information about why you should implement a shared iAd banner can be found on the Apple website at:
https://developer.apple.com/library/ios/technotes/tn2286/_index.html
This tutorial can also be viewed on youtube below:
To start of with go over to the storyboard, add two blank view controllers and set it up as folllows:
Connect the view controllers with buttons. Hold control on the button and drag them to another one and choose “show” from the options. This will set them up to load one an other.
Now that this is setup we need to create two new Swift classes, one for dog and one for cat. To do this select File->New->File. Choose Cocoa Touch class from iOS. Make one named Cat and Dog respectively, ensuring it is a subclass of UIViewController.
Once this is done, set the custom classes in the storyboard, to connect them up.
Now that our views are setup we can add the code to show our ads!
The first thing to do is go over to our app delegate. Under import UIKit we need to add import iAd. Also create a new class variable under the existing window one. It will be var UIiAD: ADBannerView = ADBannerView(). This is going to be our iAd view that will be shown on each view controller. Your code will end up looking something like below:
import UIKit import iAd @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var UIiAd: ADBannerView = ADBannerView()
Now go over to our ViewController.swift, we will ad in the code to display the ad. Repeat the same steps as above. Except we must also add ADBannerViewDelegate into the class. It will look like this:
import UIKit import iAd class ViewController: UIViewController, ADBannerViewDelegate { var UIiAd: ADBannerView = ADBannerView()
Now for the code to display the ad, add in/change the ViewController class to look like this:
import UIKit import iAd class ViewController: UIViewController, ADBannerViewDelegate { var UIiAd: ADBannerView = ADBannerView() // 1 func appdelegate() -> AppDelegate { return UIApplication.sharedApplication().delegate as! AppDelegate } // 2 override func viewWillAppear(animated: Bool) { var SH = UIScreen.mainScreen().bounds.height UIiAd.delegate = self UIiAd = self.appdelegate().UIiAd UIiAd.frame = CGRectMake(0, SH - 50, 0, 0) self.view.addSubview(UIiAd) } // 3 override func viewWillDisappear(animated: Bool) { UIiAd.delegate = nil UIiAd.removeFromSuperview() } // 4 func bannerViewDidLoadAd(banner: ADBannerView!) { UIView.beginAnimations(nil, context: nil) UIView.setAnimationDuration(1) UIiAd.alpha = 1 UIView.commitAnimations() } // 5 func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) { UIView.beginAnimations(nil, context: nil) UIView.setAnimationDuration(0) UIiAd.alpha = 1 UIView.commitAnimations() } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
The code does the following:
// 1
Return an instance of AppDelegate so we can access the shared iAd banner.
// 2
Loads a iAd at the bottom of the screen when the view is loaded
// 3
When the ViewController is no longer used it destroys the ad, freeing up memory.
// 4
Once the iAd has fully loaded it will display the iAd, this is a requirement to have iAds.
// 5
If the iAd failed to load, it is hidden from the user. This might be due to a poor internet connection, or no internet connection. This is also a requirement from Apple that must be implemented when using iAds.
Now you can save and run the application and you will see the first view controller load up an iAd!. To have iAds on the other view controllers implement the same code as above and you can run and test this. Each View Controller that uses this code will load the iAd from the App Delegate, this way if the iAd has already been downloaded it does not need to do it again, it simply access the existing iAd.