Last active
April 5, 2019 04:07
-
-
Save fx-studio/fac19924b0b39acafe3cf539f104d766 to your computer and use it in GitHub Desktop.
Example - Custom UIViewController with Google Admob
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// BaseViewController.swift | |
// CrazyQuizEnglish | |
// | |
// Created by Tien Le P. on 2/27/19. | |
// Copyright © 2019 Fx Studio All rights reserved. | |
// | |
import UIKit | |
import GoogleMobileAds | |
typealias CompletedAdmob = () -> () | |
typealias CompletedAdmobReward = (Bool) -> () | |
class BaseViewController: UIViewController { | |
var bannerView: GADBannerView! | |
var interstitial: GADInterstitial! | |
var completion: CompletedAdmob! | |
var completionReward: CompletedAdmobReward! | |
var isAdmobReward = false | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// config common | |
view.backgroundColor = App.Color.main | |
} | |
} | |
// -------------------------------------------------------------------------- // | |
//MARK: - ADMOB BANNER | |
extension BaseViewController: GADBannerViewDelegate { | |
func configAdmobBanner() { | |
if App.Key.isShowAdmob { | |
bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait) | |
bannerView.adUnitID = App.Key.admobBannerID | |
bannerView.rootViewController = self | |
bannerView.load(GADRequest()) | |
bannerView.delegate = self | |
} | |
} | |
func addBannerViewToView(_ bannerView: GADBannerView) { | |
bannerView.translatesAutoresizingMaskIntoConstraints = false | |
view.addSubview(bannerView) | |
if #available(iOS 11.0, *) { | |
// In iOS 11, we need to constrain the view to the safe area. | |
positionBannerViewFullWidthAtBottomOfSafeArea(bannerView) | |
} | |
else { | |
// In lower iOS versions, safe area is not available so we use | |
// bottom layout guide and view edges. | |
positionBannerViewFullWidthAtBottomOfView(bannerView) | |
} | |
} | |
// MARK: - view positioning | |
@available (iOS 11, *) | |
func positionBannerViewFullWidthAtBottomOfSafeArea(_ bannerView: UIView) { | |
// Position the banner. Stick it to the bottom of the Safe Area. | |
// Make it constrained to the edges of the safe area. | |
let guide = view.safeAreaLayoutGuide | |
NSLayoutConstraint.activate([ | |
guide.leftAnchor.constraint(equalTo: bannerView.leftAnchor), | |
guide.rightAnchor.constraint(equalTo: bannerView.rightAnchor), | |
guide.bottomAnchor.constraint(equalTo: bannerView.bottomAnchor) | |
]) | |
} | |
func positionBannerViewFullWidthAtBottomOfView(_ bannerView: UIView) { | |
view.addConstraint(NSLayoutConstraint(item: bannerView, | |
attribute: .leading, | |
relatedBy: .equal, | |
toItem: view, | |
attribute: .leading, | |
multiplier: 1, | |
constant: 0)) | |
view.addConstraint(NSLayoutConstraint(item: bannerView, | |
attribute: .trailing, | |
relatedBy: .equal, | |
toItem: view, | |
attribute: .trailing, | |
multiplier: 1, | |
constant: 0)) | |
view.addConstraint(NSLayoutConstraint(item: bannerView, | |
attribute: .bottom, | |
relatedBy: .equal, | |
toItem: view.safeAreaLayoutGuide, | |
attribute: .top, | |
multiplier: 1, | |
constant: 0)) | |
} | |
/// Tells the delegate an ad request loaded an ad. | |
func adViewDidReceiveAd(_ bannerView: GADBannerView) { | |
print("🔶 adViewDidReceiveAd") | |
addBannerViewToView(bannerView) | |
bannerView.alpha = 0 | |
UIView.animate(withDuration: 1, animations: { | |
bannerView.alpha = 1 | |
}) | |
} | |
/// Tells the delegate an ad request failed. | |
func adView(_ bannerView: GADBannerView, | |
didFailToReceiveAdWithError error: GADRequestError) { | |
print("🔶 adView:didFailToReceiveAdWithError: \(error.localizedDescription)") | |
} | |
/// Tells the delegate that a full-screen view will be presented in response | |
/// to the user clicking on an ad. | |
func adViewWillPresentScreen(_ bannerView: GADBannerView) { | |
print("🔶 adViewWillPresentScreen") | |
} | |
/// Tells the delegate that the full-screen view will be dismissed. | |
func adViewWillDismissScreen(_ bannerView: GADBannerView) { | |
print("🔶 adViewWillDismissScreen") | |
} | |
/// Tells the delegate that the full-screen view has been dismissed. | |
func adViewDidDismissScreen(_ bannerView: GADBannerView) { | |
print("🔶 adViewDidDismissScreen") | |
} | |
/// Tells the delegate that a user click will open another app (such as | |
/// the App Store), backgrounding the current app. | |
func adViewWillLeaveApplication(_ bannerView: GADBannerView) { | |
print("🔶 adViewWillLeaveApplication") | |
} | |
} | |
// -------------------------------------------------------------------------- // | |
//MARK: ADMOD INTERSTITIAL (FULL) | |
extension BaseViewController: GADInterstitialDelegate { | |
func configAdmobInterstitial() { | |
interstitial = createAndLoadInterstitial() | |
} | |
func showAdmobFull(completed: @escaping CompletedAdmob) { | |
self.completion = completed | |
if App.Key.isShowAdmob && interstitial != nil && interstitial.isReady { | |
interstitial.present(fromRootViewController: self) | |
} else { | |
self.completion() | |
} | |
} | |
func createAndLoadInterstitial() -> GADInterstitial? { | |
let number = Int.random(in: 0...2) | |
print("⚪️ random show AD: \(number)") | |
if number != 2 { return nil } | |
let interstitial = GADInterstitial(adUnitID: App.Key.admobInterstitialID) | |
interstitial.delegate = self | |
interstitial.load(GADRequest()) | |
return interstitial | |
} | |
/// Tells the delegate an ad request succeeded. | |
func interstitialDidReceiveAd(_ ad: GADInterstitial) { | |
print("⚪️ interstitialDidReceiveAd") | |
} | |
/// Tells the delegate an ad request failed. | |
func interstitial(_ ad: GADInterstitial, didFailToReceiveAdWithError error: GADRequestError) { | |
print("⚪️ interstitial:didFailToReceiveAdWithError: \(error.localizedDescription)") | |
self.completion() | |
} | |
/// Tells the delegate that an interstitial will be presented. | |
func interstitialWillPresentScreen(_ ad: GADInterstitial) { | |
print("⚪️ interstitialWillPresentScreen") | |
} | |
/// Tells the delegate the interstitial is to be animated off the screen. | |
func interstitialWillDismissScreen(_ ad: GADInterstitial) { | |
print("⚪️ interstitialWillDismissScreen") | |
} | |
/// Tells the delegate the interstitial had been animated off the screen. | |
func interstitialDidDismissScreen(_ ad: GADInterstitial) { | |
print("⚪️ interstitialDidDismissScreen") | |
self.completion() | |
} | |
/// Tells the delegate that a user click will open another app | |
/// (such as the App Store), backgrounding the current app. | |
func interstitialWillLeaveApplication(_ ad: GADInterstitial) { | |
print("⚪️ interstitialWillLeaveApplication") | |
} | |
} | |
// -------------------------------------------------------------------------- // | |
//MARK: ADMOB REWARD | |
extension GameViewController: GADRewardBasedVideoAdDelegate { | |
func configAdmobReward() { | |
GADRewardBasedVideoAd.sharedInstance().delegate = self | |
GADRewardBasedVideoAd.sharedInstance().load(GADRequest(), withAdUnitID: App.Key.admobRewarID) | |
} | |
func showAdmobRewardVideo(completion: @escaping CompletedAdmobReward) { | |
self.completionReward = completion | |
self.isAdmobReward = false | |
if GADRewardBasedVideoAd.sharedInstance().isReady == true { | |
GADRewardBasedVideoAd.sharedInstance().present(fromRootViewController: self) | |
} else { | |
self.completionReward(false) | |
} | |
} | |
func reloadRewardVideo() { | |
if GADRewardBasedVideoAd.sharedInstance().isReady == false { | |
GADRewardBasedVideoAd.sharedInstance().load(GADRequest(), withAdUnitID: App.Key.admobRewarID) | |
} | |
} | |
// Delegate | |
func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd, | |
didRewardUserWith reward: GADAdReward) { | |
print("🔵 Reward received with currency: \(reward.type), amount \(reward.amount).") | |
self.isAdmobReward = true | |
} | |
func rewardBasedVideoAdDidReceive(_ rewardBasedVideoAd:GADRewardBasedVideoAd) { | |
print("🔵 Reward based video ad is received.") | |
} | |
func rewardBasedVideoAdDidOpen(_ rewardBasedVideoAd: GADRewardBasedVideoAd) { | |
print("🔵 Opened reward based video ad.") | |
} | |
func rewardBasedVideoAdDidStartPlaying(_ rewardBasedVideoAd: GADRewardBasedVideoAd) { | |
print("🔵 Reward based video ad started playing.") | |
} | |
func rewardBasedVideoAdDidCompletePlaying(_ rewardBasedVideoAd: GADRewardBasedVideoAd) { | |
print("🔵 Reward based video ad has completed.") | |
} | |
func rewardBasedVideoAdDidClose(_ rewardBasedVideoAd: GADRewardBasedVideoAd) { | |
print("🔵 Reward based video ad is closed.") | |
self.completionReward(isAdmobReward) | |
} | |
func rewardBasedVideoAdWillLeaveApplication(_ rewardBasedVideoAd: GADRewardBasedVideoAd) { | |
print("🔵 Reward based video ad will leave application.") | |
} | |
func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd, | |
didFailToLoadWithError error: Error) { | |
print("🔵 Reward based video ad failed to load.") | |
self.completionReward(false) | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// call Admob | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Admob | |
self.configAdmobBanner() | |
self.configAdmobReward() | |
self.configAdmobInterstitial() | |
} | |
// show Admob Reward | |
self.showAdmobRewardVideo { (isReward) in | |
if isReward { | |
// continue the game | |
} else { | |
// game over | |
} | |
} | |
// show Admob interstjtial | |
self.showAdmobFull { | |
self.dismiss(animated: true) { | |
print("END GAME") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment