GADBannerView Singleton- What goes into which ViewController?

958 views
Skip to first unread message

Adem Redzic

unread,
Feb 26, 2016, 10:20:15 PM2/26/16
to Google Mobile Ads SDK Developers
I wish there was a video tutorial on how to do this. I always get some kind of error even though I create the classes correctly. They are usually undeclared identifier errors or 'expected' errors. Can someone explain how to implement a singleton as though I'm 5?

Veer Arjun Busani

unread,
Feb 29, 2016, 9:55:22 AM2/29/16
to Google Mobile Ads SDK Developers
Hi Adem,

You need to create a Singleton class first for which you can follow any of these patterns. Next import the Mobile Ads framework to write a function to create Banner Ads.  Finally in your View Controller, call this function, while sending its residing UIView and make the GADRequest for ads. For example - 

In your Singleton class - 

class BannerManager: NSObject {
    private var bannerView: GADBannerView = GADBannerView()

    class var sharedManager: BannerManager {
        struct Static {
            static var instance: BannerManager?
            static var token: dispatch_once_t = 0
        }

        dispatch_once(&Static.token) {
            Static.instance = BannerManager()
        }

        return Static.instance!
    }

    func setupBannerAds(view: UIView) -> GADBannerView {
        bannerView.adSize = kGADAdSizeSmartBannerPortrait
        view.addSubview(bannerView)

        bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
        bannerView.translatesAutoresizingMaskIntoConstraints = false

        let widthConstraint = NSLayoutConstraint(item: bannerView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 320)
        view.addConstraint(widthConstraint)

        let heightConstraint = NSLayoutConstraint(item: bannerView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 50)
        view.addConstraint(heightConstraint)

        let horizontalConstraint = NSLayoutConstraint(item: bannerView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
        view.addConstraint(horizontalConstraint)

        let bottomConstraint = NSLayoutConstraint(item: bannerView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)
        view.addConstraint(bottomConstraint)

        return bannerView
    }
}

And in your UIViewController - 

class ViewController: UIViewController, GADBannerViewDelegate {

    let bManager = BannerManager.sharedManager

    override func viewDidLoad() {
        super.viewDidLoad()

        let banner = bManager.setupBannerAds(view)
        banner.delegate = self
        banner.rootViewController = self

        let request: GADRequest = GADRequest()
        request.testDevices = [kGADSimulatorID]
        banner.loadRequest(GADRequest())
    }

    func adViewDidReceiveAd(bannerView: GADBannerView!) {
        print("Reveived Ad : \(bannerView.adNetworkClassName)")
    }
}
Thanks,
Veer Arjun Busani
Mobile Ads SDK Team
Message has been deleted

Adem Redzic

unread,
Mar 1, 2016, 7:50:09 PM3/1/16
to Google Mobile Ads SDK Developers
Hi Veer,

Thank you so much for the detailed response! I'm not able to write this code until the weekend but I will get back to you when I do.

I really appreciate the explanation.
-Adem

Adem Redzic

unread,
Mar 2, 2016, 8:06:59 PM3/2/16
to Google Mobile Ads SDK Developers
Okay so I'm still getting issues preventing me from building the app. Is this coded in Swift? If so, I should mention I'm using Objective C. For the singleton link you gave me I used the Objective C code and put it in ViewController.m and it builds just fine. But for the code you gave me I get errors whether I put it in ViewController.h or ViewController.m. It's usually the "expected identifier (" error that doesn't resolve itself ever. 

Also if it helps I have AdMob setup properly in my app and can receive ads, but just need to get the singleton working so the banner doesn't reload and the ad shows up correctly across all view controllers.

-Adem

Veer Arjun Busani

unread,
Mar 3, 2016, 10:31:38 AM3/3/16
to Google Mobile Ads SDK Developers
Hi Adem,

I'm attaching two Objective-C files - View Controller and a Singleton. You must be able to receive Banner Ads through them. It is the same code in Objective-C.

Thanks,
Veer Busani
Mobile Ads SDK Team
Singleton class for MAD.zip

Adem Redzic

unread,
Mar 3, 2016, 9:17:49 PM3/3/16
to Google Mobile Ads SDK Developers
Thank you, the code works but the problem is the ad reloads every time I go to a new view controller. I wanted the ad to stay the same on all view controllers to maximize my impressions. I tried the files in a new project, with the only changes being that I added the Google Ad framework and I set every view controller class to "ViewController." I made 3 view controllers for this test and I'm getting the same results as the first time.

Any help would be appreciated.

-Adem

Veer Arjun Busani

unread,
Mar 4, 2016, 10:24:52 AM3/4/16
to Google Mobile Ads SDK Developers
Hi Adem,

Now I understand why you wanted to use a Singleton pattern for showing Banner Ads. You would not be able to load the same ad, using a Singleton, over multiple View Controllers. This is because the Banner AdView needs to know the presenting View Controller for it load the Ads and even with Singletons, these AdViews are being added back to the ViewController. Having said that, I would suggest you to create one MasterViewController with one banner ad and then load all of your View Controllers as Containers.

Thanks,
Veer Arjun Busani
Mobile Ads SDK Team

Adem Redzic

unread,
Mar 4, 2016, 2:49:47 PM3/4/16
to Google Mobile Ads SDK Developers
Is there a way to mimic the setup of multiple view controllers with containers? Like having buttons to go to different containers much like view controllers?

Veer Arjun Busani

unread,
Mar 4, 2016, 4:07:09 PM3/4/16
to Google Mobile Ads SDK Developers
Hi Adem,

Well it really depends on your implementation. I personally would rather make a new AdRequest for each of the View Controller as it would still count as impression regardless. Also the AdView auto refreshes(depending on the setting in your AdMob, default is 60 seconds) for a new ad(a new AdRequest). So it does not make much sense have a single AdView throughout the application.

Adem Redzic

unread,
Mar 4, 2016, 4:46:05 PM3/4/16
to Google Mobile Ads SDK Developers
Okay, I was worried that it wouldn't count as an impression because of how quickly I expect users to switch through view controllers. I guess the only annoyance is the ad disappears for a few seconds before coming back every switch. Still it's been a learning experience and I'm grateful you helped me through it.

All the best,

-Adem Redzic
Reply all
Reply to author
Forward
0 new messages