1

I am looking to load one instance of a Google Admob Banner View throughout multiple View Controllers (including, but not limited to a UITabBarController.)

My attempt is below. I'm using AppDelegate to set the adSize, adUnitID and testDevices. Then in each VC where I want a banner displayed, I set the rootViewController, frame, load request, and then addSubView.

This works, in the fact that the ads show up fine. However, the ads keep changing when I segue or dismiss VC! It appears that a new request is happening everytime VC's change. Which is precisely the result that must be avoided!

AppDelegate

class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    var adBannerViewFromAppDelegate = GADBannerView()
    let loadRequest = GADRequest()

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

    adBannerViewFromAppDelegate.adSize = kGADAdSizeSmartBannerPortrait
    adBannerViewFromAppDelegate.adUnitID = "12345"
    loadRequest.testDevices = [kGADSimulatorID, myiPhone]
    }
}

ViewController (This has a button to SecondViewController via Push Segue)

import UIKit
import GoogleMobileAds

class ViewController: UIViewController {

    let appDelegate = UIApplication.shared.delegate as! AppDelegate

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

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        addBannerToView()
    }

    func addBannerToView() {
        appDelegate.adBannerViewFromAppDelegate.rootViewController = self
        appDelegate.adBannerViewFromAppDelegate.load(appDelegate.loadRequest)
        appDelegate.adBannerViewFromAppDelegate.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: appDelegate.adBannerViewFromAppDelegate.frame.height)
        view.addSubview(appDelegate.adBannerViewFromAppDelegate)
    }

}

SecondViewController

import UIKit
import GoogleMobileAds

class SecondViewController: UIViewController {

    let appDelegate = UIApplication.shared.delegate as! AppDelegate

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

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        addBannerToView()
    }

    @IBAction func closeButtonPressed(_ sender: UIButton) {
        self.dismiss(animated: true, completion: nil)
    }

    func addBannerToView() {
        appDelegate.adBannerViewFromAppDelegate.rootViewController = self
        appDelegate.adBannerViewFromAppDelegate.load(appDelegate.loadRequest)
        appDelegate.adBannerViewFromAppDelegate.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: appDelegate.adBannerViewFromAppDelegate.frame.height)
        view.addSubview(appDelegate.adBannerViewFromAppDelegate)
    }

}

How can I get one instance of the Banner created in the AppDelegate to display on multiple ViewControllers? Thanks.

Joe
  • 3,772
  • 3
  • 33
  • 64
  • try this https://github.com/DanielStormApps/Shared-AdMob-Interstitial/tree/master/SharedAdMobInterstitial similar logic should work for banner ad – Amod Gokhale Jun 10 '17 at 08:18
  • @Amod Gokhale - it doesn't work, because that repo is calling the load request in appdelegate. when you do that with banners, you get a console warning: ' You must set the rootViewController property of before loading a request.' – Joe Jun 11 '17 at 22:39
  • looks like this solution works but get same warning. Try it and if fails i recommend you to submit a bug request with google to make viewdelege weak reference? May be they will provide a better solution https://stackoverflow.com/questions/39572974/how-to-add-an-admob-gadbannerview-to-every-view and – Amod Gokhale Jun 12 '17 at 05:46
  • any case please update your answer with final solution you find . it will help others as well – Amod Gokhale Jun 12 '17 at 05:47

0 Answers0