2

I am implementing an AdMob banner in my app for a single UIViewController, and it is working. But I have so many views and I want to show a banner on every screen. How would I implement a banner that would appear on every screen? I am trying this in my AppDelegate.swift:

dispatch_async(dispatch_get_main_queue(), {
    print("Google Mobile Ads SDK version: \(GADRequest.sdkVersion())")
    // bannerView.frame = CGRectMake(0, 0, 320, 50)
    // self.bannerView.adSize = kGADAdSizeBanner
    self.bannerView.adUnitID = "ca-app-pub-MY_ID"
    bannerView.rootViewController = self.window
    self.bannerView.loadRequest(GADRequest())
    self.window?.addSubview(self.bannerView)
})

but its not working

Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
Museer Ahamad Ansari
  • 5,414
  • 3
  • 39
  • 45
  • 2
    it is impossible, you have to define the `BannerView` in every `UIViewController` – Özgür Ersil Sep 19 '16 at 12:21
  • @Özgür Ersil , can you send me a link or explain me why? Thanks – Museer Ahamad Ansari Sep 19 '16 at 12:56
  • Look at one of these links. [Stackoverflow iAdBanner between views](http://stackoverflow.com/questions/28904619/how-to-share-iad-banner-between-views-using-appdelegate) or this [iAdExample](https://github.com/perlfly/iAdExample) – MwcsMac Sep 19 '16 at 14:16
  • 1
    @ÖzgürErsil not necessarily. You just have to position the banner for each View Controller. Check my answer: http://stackoverflow.com/a/39575849/2108547 – Daniel Storm Sep 19 '16 at 14:41

2 Answers2

3

Create a shared banner. You init it in your AppDelegate and then add it to the UIViewController's that you'd like to have a banner on:

class AppDelegate: UIResponder, UIApplicationDelegate, GADBannerViewDelegate {

var window: UIWindow?
var adBannerView = GADBannerView()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    adBannerView.adUnitID = "YourAdUnitID"
    adBannerView.delegate = self
    adBannerView.load(GADRequest())
    adBannerView.isHidden = true

    return true
}

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

In each UIViewController you'd like to have a banner:

let appDelegate = UIApplication.shared.delegate as! AppDelegate

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

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
  • but here you just make 1 banner request for each window – Özgür Ersil Sep 19 '16 at 14:43
  • @ÖzgürErsil There is only one request happening once the app launches. The banner will refresh at what ever interval they have set on AdMob.com. No need to send more requests. – Daniel Storm Sep 19 '16 at 14:46
  • 4
    @DanielStorm i am getting this warning in console. **You must set the rootViewController property of > before loading a request.** – Museer Ahamad Ansari Sep 20 '16 at 07:18
  • @DanielStorm - I'm getting the same error as Museer. rootViewController needs to be set before an ad request gets made. What's the solution? – Joe Jun 08 '17 at 00:05
0

SWIFT 3

func showAds(viewAds:GADBannerView,viewController:UIViewController){
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.adBannerView.adSize = kGADAdSizeBanner
    appDelegate.adBannerView.rootViewController = viewController
    appDelegate.adBannerView.frame = CGRect(x: 0,
                                            y: 0,
                                            width: viewAds.frame.width,
                                            height: viewAds.frame.height)
    viewAds.addSubview(appDelegate.adBannerView)
}


showAds(viewAds: adseHere, viewController: self)
Ahmed Safadi
  • 4,402
  • 37
  • 33
  • Is it possible display different Ads on each recurring items? – Priya Jun 22 '20 at 01:23
  • what do you mean by different Ads? – Ahmed Safadi Jun 23 '20 at 08:55
  • I am getting same Ads in each recurring items. I would like to show each different Ads on each recurring items. https://stackoverflow.com/questions/62485567/ios-swift-google-addmob-sdk-how-to-display-different-ads-in-recurring-items-in Appreciated if you post your answer here. Thanks in advance – Priya Jun 25 '20 at 07:06