1

I am using AppDelegate to configure a banner view, and its delegates. Then displaying the banner on two VC's: ViewController, and SecondViewController. The banners show up properly, but with two visual mishaps.

  1. On app open, there's about a 2-3 second delay for the test banner to load up. When I was using IB, the banners showed up instantly.

  2. When transitioning from ViewController -> SecondViewController via segue (Present Modally / Over Full Screen / Cross Disolve / No Animation), there's a delay of about 1-2 seconds till the banner shows.

The banners load up perfectly, minus the two lag / delay issues above. How can I resolve these?

Storyboard: Storyboard AppDelegate

class AppDelegate: UIResponder, UIApplicationDelegate, GADBannerViewDelegate {

var window: UIWindow?
var adBannerView = GADBannerView()
let myBannerRequest = GADRequest()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    myBannerRequest.testDevices = [kGADSimulatorID]
    adBannerView.delegate = self
    adBannerView.isHidden = true

    return true
}

func adViewDidReceiveAd(_ bannerView: GADBannerView!) {
    adBannerView.isHidden = false
}
func adView(_ bannerView: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!) {
    adBannerView.isHidden = true
}
}

ViewController

let appDelegate = UIApplication.shared.delegate as! AppDelegate

override func viewDidLoad() {
    super.viewDidLoad()
    requestAds()
}

internal func requestAds() {
    appDelegate.adBannerView = GADBannerView()
    appDelegate.adBannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
    appDelegate.adBannerView.adSize = kGADAdSizeBanner
    appDelegate.adBannerView.rootViewController = self
    appDelegate.adBannerView.load(appDelegate.myBannerRequest)
    appDelegate.adBannerView.center = CGPoint(x: view.frame.midX, y: view.frame.height - appDelegate.adBannerView.frame.height / 2)
    view.addSubview(appDelegate.adBannerView)
}

SecondViewController

// Same as ViewController, with addition of IBAction for the button:
@IBAction func closeButtonPressed(_ sender: UIButton) {
    self.dismiss(animated: true, completion: nil)
}
Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
Joe
  • 3,772
  • 3
  • 33
  • 64

1 Answers1

1

Looks like you're trying to create one GADBannerView to use across your entire application, which is a good idea. You're not doing that though because you're creating a new GADBannerView in your View Controller's requestAds function: appDelegate.adBannerView = GADBannerView(). Remove it.

Also, do your request and set your Ad Unit ID in your app delegate and remove it from your requestAds function. In your View Controller's viewDidLoad you should only be positioning your GADBannerView and adding it to the view.

I imagine you would like the banner to fill the width of the screen too so I've changed the frame property. If you wanted a function for this it would end up similar to:

func addBannerToView() {
    appDelegate.adBannerView.adSize = kGADAdSizeBanner
    appDelegate.adBannerView.rootViewController = self
    appDelegate.adBannerView.frame = CGRect(x: 0.0,
                          y: view.frame.height - appDelegate.adBannerView.frame.height,
                          width: view.frame.width,
                          height: appDelegate.adBannerView.frame.height)
    view.addSubview(appDelegate.adBannerView)
}
Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
  • Loads up great / Loads up on Second VC great... When clicking the close button, the ad isn't there anymore. I've experienced this before I made my initial post, that's why I had appDelegate.adBannerView = GADBannerView() in there. To try loading another one. Is there a fix for the banners not showing up when the second VC is dismissed? – Joe Sep 19 '16 at 23:00
  • Instead of viewDidLoad call the function in viewDidAppear – Daniel Storm Sep 19 '16 at 23:02
  • That works! Got an even better result (no ad flicker) using viewWillAppear. Thanks for your help here. – Joe Sep 20 '16 at 03:44