1

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)
}
Community
  • 1
  • 1
ErkanA
  • 179
  • 1
  • 11

2 Answers2

3

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

beyowulf
  • 15,101
  • 2
  • 34
  • 40
  • this has done the trick at first but what I am trying to do is showing the same ad in all views. If I put self.canDisplayBannerAds = true to only one view, ad loads at first but changing the views makes the banner empty. After renewal of the ad, it becomes active again. Putting ad code to all views doesn't change anything – ErkanA Nov 05 '15 at 12:39
  • 1
    You can use that code to load ads in all your view controllers and the system will match them. If you segue away from a view controller and you are handling the adBanner's view yourself, you will probably want to animate the banner off the screen in viewDidDisappear or in prepareForSegue. – beyowulf Nov 05 '15 at 12:43
  • What happens if I add self.canDisplayBannerAds = true to all views? Actually I meant doing it without removing from screen. I just added it to all views and it seems to be working ok, but will this fit for 30 secs rule? – ErkanA Nov 05 '15 at 13:28
  • 1
    30 second rule? I don't know what you're talking about. If you are trying to game iAd to increase revenue without showing ads, they will know and stop serving ads. If you have questions about iAd, I encourage to watch the video and read Apple's documentation. – beyowulf Nov 05 '15 at 13:42
  • Isn't there a 30secs rule that enforce you to show the ad for at least 30 secs before counting as an impression? By the way, I might be wrong, I am on my first own project. https://developer.apple.com/library/ios/technotes/tn2264/_index.html – ErkanA Nov 05 '15 at 13:48
  • 1
    That's not about counting an impression. The amount of time an ad must be displayed in order for it to count as an impression is undefined. These kind of particularities are abstracted, if you use the API the way Apple would like you. If you display a banner in one view controller and move to the next the same banner will be displayed there. iAd will update the banner when necessary. – beyowulf Nov 05 '15 at 14:47
  • Thank you very much for your help. – ErkanA Nov 05 '15 at 18:32
1

I think you add your tableview after adding the bannerView in your VC, so the tableView covers the bannerView, you can adjust bannerView's zPosition to re-order them, add this line in your viewWillAppear:

adBannerView.layer.zPosition = 10

By the way,i think this process should be done in ViewDidLoad.

duan
  • 8,515
  • 3
  • 48
  • 70