Skip to content

Instantly share code, notes, and snippets.

@mspvirajpatel
Created September 14, 2017 13:48
Show Gist options
  • Save mspvirajpatel/e3b15ad52c20254c8e2683565064630a to your computer and use it in GitHub Desktop.
Save mspvirajpatel/e3b15ad52c20254c8e2683565064630a to your computer and use it in GitHub Desktop.
Easily integration banner and interstitial GoogleMobileAds in swift project.
import UIKit
import GoogleMobileAds
//MARK: - Google Ads Unit ID
struct GoogleAdsUnitID {
//Google Test Unit ID
struct Test {
static var strBannerAdsID = "ca-app-pub-XXXXXXXXXXXXXXXX/XXXXXXXXXXX"
static var strInterstitialAdsID = "ca-app-pub-XXXXXXXXXXXXXXXX/XXXXXXXXXXX"
}
//Google Live admob Unit ID
struct Live {
static var strBannerAdsID = "ca-app-pub-XXXXXXXXXXXXXXXX/XXXXXXXXXXX"
static var strInterstitialAdsID = "ca-app-pub-XXXXXXXXXXXXXXXX/XXXXXXXXXXX"
}
}
//MARK: - Banner View Size
struct BannerViewSize {
static var screenWidth = UIScreen.main.bounds.size.width
static var screenHeight = UIScreen.main.bounds.size.height
static var height = CGFloat((UIDevice.current.userInterfaceIdiom == .pad ? 90 : 50))
}
//MARK: - Create GoogleAdMob Class
class GoogleAdMob:NSObject, GADInterstitialDelegate, GADBannerViewDelegate {
//MARK: - Shared Instance
static let sharedInstance : GoogleAdMob = {
let instance = GoogleAdMob()
return instance
}()
//MARK: - Variable
private var isBannerViewDisplay = false
private var isInitializeBannerView = false
private var isInitializeInterstitial = false
private var isBannerLiveID = false
private var isInterstitialLiveID = false
private var interstitialAds: GADInterstitial!
private var bannerView: GADBannerView!
//MARK: - Create Banner View
func initializeBannerView(isLiveUnitID:Bool) {
self.isInitializeBannerView = true
self.isBannerLiveID = isLiveUnitID
self.createBannerView()
}
@objc private func createBannerView() {
print("GoogleAdMob : create")
if UIApplication.shared.keyWindow?.rootViewController == nil {
NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(createBannerView), object: nil)
self.perform(#selector(createBannerView), with: nil, afterDelay: 0.5)
} else {
isBannerViewDisplay = true
bannerView = GADBannerView(frame: CGRect(
x:0 ,
y:BannerViewSize.screenHeight - BannerViewSize.height ,
width:BannerViewSize.screenWidth ,
height:BannerViewSize.height))
if self.isBannerLiveID == false {
self.bannerView.adUnitID = GoogleAdsUnitID.Test.strBannerAdsID
} else {
self.bannerView.adUnitID = GoogleAdsUnitID.Live.strBannerAdsID
}
self.bannerView.rootViewController = UIApplication.shared.keyWindow?.rootViewController
self.bannerView.delegate = self
self.bannerView.backgroundColor = .gray
self.bannerView.load(GADRequest())
UIApplication.shared.keyWindow?.addSubview(bannerView)
}
}
//MARK: - Hide - Show Banner View
func showBannerView() {
print("showBannerView")
isBannerViewDisplay = true
if isInitializeBannerView == false {
print("First initialize Banner View")
} else {
print("isBannerViewCreate : true")
self.bannerView.isHidden = false
UIView.animate(withDuration: 0.3, animations: {
self.bannerView.frame = CGRect(x:0 ,y:BannerViewSize.screenHeight - BannerViewSize.height ,width:BannerViewSize.screenWidth ,height:BannerViewSize.height)
})
}
}
func hideBannerView() {
print("hideBannerView")
isBannerViewDisplay = false
if self.bannerView != nil {
UIView.animate(withDuration: 0.3, animations: {
self.bannerView.frame = CGRect(x:0 ,y:BannerViewSize.screenHeight ,width:BannerViewSize.screenWidth ,height:BannerViewSize.height)
})
}
}
@objc private func showBanner() {
print("showBanner")
if self.bannerView != nil && isBannerViewDisplay == true {
self.bannerView.isHidden = false
}
}
private func hideBanner() {
print("hideBanner")
if self.bannerView != nil {
self.bannerView.isHidden = true
}
}
//MARK: - GADBannerView Delegate
func adViewDidReceiveAd(_ bannerView: GADBannerView) {
print("adViewDidReceiveAd")
}
func adViewDidDismissScreen(_ bannerView: GADBannerView) {
print("adViewDidDismissScreen")
}
func adViewWillDismissScreen(_ bannerView: GADBannerView) {
print("adViewWillDismissScreen")
}
func adViewWillPresentScreen(_ bannerView: GADBannerView) {
print("adViewWillPresentScreen")
}
func adViewWillLeaveApplication(_ bannerView: GADBannerView) {
print("adViewWillLeaveApplication")
}
func adView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: GADRequestError) {
print("adView")
}
//MARK: - Create Interstitial Ads
func initializeInterstitial(isLiveUnitID:Bool) {
self.isInitializeInterstitial = true
self.isInterstitialLiveID = isLiveUnitID
self.createInterstitial()
}
private func createInterstitial() {
if self.isInterstitialLiveID == false {
interstitialAds = GADInterstitial(adUnitID: GoogleAdsUnitID.Test.strInterstitialAdsID)
} else {
interstitialAds = GADInterstitial(adUnitID: GoogleAdsUnitID.Live.strInterstitialAdsID)
}
interstitialAds.delegate = self
interstitialAds.load(GADRequest())
}
//MARK: - Show Interstitial Ads
func showInterstitial() {
if isInitializeInterstitial == false {
print("First initialize Interstitial")
} else {
if interstitialAds.isReady {
interstitialAds.present(fromRootViewController: (UIApplication.shared.keyWindow?.rootViewController)!)
} else {
print("Interstitial not ready")
self.createInterstitial()
}
}
}
//MARK: - GADInterstitial Delegate
func interstitialDidReceiveAd(_ ad: GADInterstitial) {
print("interstitialDidReceiveAd")
}
func interstitialDidDismissScreen(_ ad: GADInterstitial) {
print("interstitialDidDismissScreen")
self.createInterstitial()
}
func interstitialWillDismissScreen(_ ad: GADInterstitial) {
print("interstitialWillDismissScreen")
NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(showBanner), object: nil)
self.perform(#selector(showBanner), with: nil, afterDelay: 0.1)
}
func interstitialWillPresentScreen(_ ad: GADInterstitial) {
print("interstitialWillPresentScreen")
self.hideBanner()
}
func interstitialWillLeaveApplication(_ ad: GADInterstitial) {
print("interstitialWillLeaveApplication")
}
func interstitialDidFail(toPresentScreen ad: GADInterstitial) {
print("interstitialDidFail")
}
func interstitial(_ ad: GADInterstitial, didFailToReceiveAdWithError error: GADRequestError) {
print("interstitial")
}
}
###Add Banner and Interstitial Ads Unit ID in GoogleAdsMob.swift
//Google Test Unit ID for testing ads
struct Test {
static var strBannerAdsID = "ca-app-pub-XXXXXXXXXXXXXXXX/XXXXXXXXXXX"
static var strInterstitialAdsID = "ca-app-pub-XXXXXXXXXXXXXXXX/XXXXXXXXXXX"
}
//Google Live admob Unit ID for live ads
struct Live {
static var strBannerAdsID = "ca-app-pub-XXXXXXXXXXXXXXXX/XXXXXXXXXXX"
static var strInterstitialAdsID = "ca-app-pub-XXXXXXXXXXXXXXXX/XXXXXXXXXXX"
}
###Initialization Memory
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//if isLiveUnitID passing parameter false than displaying test ads and if isLiveUnitID passing parameter true than displaying live ads.
GoogleAdMob.sharedInstance.initializeInterstitial(isLiveUnitID: false)
GoogleAdMob.sharedInstance.initializeBannerView(isLiveUnitID: false)
return true
}
###Show Interstitial Ads
GoogleAdMob.sharedInstance.showInterstitial()
###Show Banner Ads
GoogleAdMob.sharedInstance.showBannerView()
###Hide Banner Ads
GoogleAdMob.sharedInstance.hideBannerView()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment