Steve Steve - 6 months ago 26
Swift Question

AdMob taking a long time to generate

SWIFT - AdMob

I have a

tabBarController
with 4 views, 3 of which I have an
GADBanner
displayed on them. They are taking about a minute before the test ad appears. In another one of my apps it generates within a couple of seconds, but that is on a single view.

I have implemented the
loadBanner
function in my app delegate so that it doesn't generate a new ad when the view changes. It worked fine with iAd but not with AdMob.

In my
appDelegate.swift
file I call the following function in
didFinishLaunchingWithOptions
:

func loadBanner() {

let testAdId = "ca-app-pub-3940256099942544/2934735716"

banner = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
banner.adUnitID = testAdId

let req : GADRequest = GADRequest()
banner.loadRequest(req)
banner.frame = CGRectMake(100, 100, banner.frame.size.width, banner.frame.size.height)
}


and then in
viewWillAppear
for each view controller I have the following:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let VCBanner = appDelegate.banner
VCBanner.rootViewController = self
VCBanner.center = CGPoint(x: view.frame.midX, y: view.frame.height - VCBanner.frame.height / 2)
view.addSubview(VCBanner)


So it works great, it just takes so long to load that the user will probably be finished!

Any ideas why this is happening?

Answer

So as I had no luck reaching out. I came up with another solution which works fine, not sure if its the best way but here it is.

In the initial ViewController (the one that appears first). I declared a global variable var banner : GADBannerView! and in viewDidAppear() I ran the function loadBanner():

let testAdId = "ca-app-pub-3940256099942544/2934735716"

func loadBanner() {
    banner = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
    banner.adUnitID = testAdId

    banner.rootViewController = self
    let req : GADRequest = GADRequest()
    banner.loadRequest(req)
    banner.frame = CGRectMake(0, self.view.frame.height - banner.frame.height, banner.frame.size.width, banner.frame.size.height)

    self.view.addSubview(banner)
}

Now as it is a global variable (declared outside the class), it is accessible from all viewControllers. So now in each of the viewControllers I want to display the ad in I can just call in viewDidAppear() like this:

self.view.addSubview(banner)

and this is working just how I was hoping!

Comments