I need to insert an AdBannerView
to a TableViewController
and position should be be bottom of the screen. I used this post as a guide which is written by Daniel Storm
The ad is shown but even there is only 1 cell in the tableview
, I need to scroll down to be able to see it. Also I would like the ad to react to screen rotation.
AppDelegate.swift
import UIKit
import iAd // Import iAd
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, ADBannerViewDelegate {
// Include the delegate for our banner
var window: UIWindow?
var adBannerView = ADBannerView() // Create our one ADBannerView
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Set delegate and hide banner initially
adBannerView.delegate = self
adBannerView.hidden = true
return true
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
print("bannerViewDidLoadAd")
adBannerView.hidden = false
}
func bannerViewActionDidFinish(banner: ADBannerView!) {
print("bannerViewActionDidFinish")
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
print("didFailToReceiveAdWithError: \(error)")
adBannerView.hidden = true
}
ViewController.swift (tableviewcontroller in my case)
import UIKit
class ViewController: UIViewController {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate // Create reference to our app delegate
override func viewWillAppear(animated: Bool) {
// Position
appDelegate.adBannerView.center = CGPoint(x: view.frame.midX,
y: view.frame.height - appDelegate.adBannerView.frame.height / 2)
// Add to view
view.addSubview(appDelegate.adBannerView)
}
If all you want is a banner ad at the bottom of the screen, you can simply say something like
override func viewDidLoad() {
super.viewDidLoad()
self.canDisplayBannerAds = true
}
And it will work automagically as they say.
I would recommend watching this video to understand the iAd API