简体   繁体   中英

How to have Admob ads in multiple Viewcontrollers - Swift, Xcode?

I am new to making apps. I am currently working on an app and I put a banner ad on the first view controller and it works. I can't find a way to add the ad to the rest of the view controllers. If someone can show me how that would be great!

Check out this code, if you need I can post with interstitial support too:

import Foundation
import UIKit
import GoogleMobileAds

protocol AdSimpleBannerPowered: UIViewController {
    var placeholder: UIView? { get }
    func addBannerToAdsPlaceholder(_ banner: UIView)
}

extension AdSimpleBannerPowered {
    func addBannerToAdsPlaceholder(_ banner: UIView) {
        placeholder?.addSubview(banner)
    }
}

private struct AdsConstants {
    static let admobAppID = "YOUR_APP_ID"
    static let adBottomBannerUnitID = "YOUR_BANNER_ID"
}

final class AdsManager : NSObject {
        
    var loadedSimpleBannerAd = false
    
    private var bannerView: GADBannerView?
    var rootViewController: UIViewController? {
        didSet {
            setupSimpleBannerAdsIfPossible()
        }
    }

    public override init() {
        super.init()
        
        GADMobileAds.sharedInstance().start()
        
        configureSimpleBanner()
    }
    
    private func configureSimpleBanner() {
        bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
        bannerView?.delegate = self
        bannerView?.adUnitID = AdsConstants.adBottomBannerUnitID
    }
    
    private func setupSimpleBannerAdsIfPossible() {
        assert(self.bannerView != nil, "WTF: simple banner has not been configured (call Ads.configure() before any usage)!")
        if let root = rootViewController as? AdSimpleBannerPowered {
            if let banner = self.bannerView {
                banner.rootViewController = root
                if !loadedSimpleBannerAd {
                    banner.load(GADRequest())
                } else {
                    root.addBannerToAdsPlaceholder(banner)
                }
            }
        }
    }
    
}

// Simple Bottom Admob Banner delegate methods
extension AdsManager: GADBannerViewDelegate {
    
    func adViewDidReceiveAd(_ bannerView: GADBannerView) {
        print("adViewDidReceiveAd")
        loadedSimpleBannerAd = true
        if let root = rootViewController as? AdSimpleBannerPowered {
            root.addBannerToAdsPlaceholder(bannerView)
        }
    }
    
    func adView(_ bannerView: GADBannerView,
                didFailToReceiveAdWithError error: GADRequestError) {
        print("adView:didFailToReceiveAdWithError: \(error.localizedDescription)")
    }
    
}

Idea is to share one GADBannerView object across the app - so you don't spam Admob with every banner instantiation at each UIViewController

Usage

In each UIViewController you want the banner to be visible:

  1. Conform to AdSimpleBannerPowered protocol
  2. Add placeholder view at Storyboard or via code
  3. Add Ads.rootViewController = self at viewWillAppear() method – that will instantiate and add the banner to placeholder.
class MyViewController: UIViewController, AdSimpleBannerPowered {
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        Ads.rootViewController = self
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM