Skip to content

Instantly share code, notes, and snippets.

@MichaelBarney
Created October 22, 2019 12:15
Show Gist options
  • Save MichaelBarney/27bb6f629cfaed3ba3d02cc533124fb1 to your computer and use it in GitHub Desktop.
Save MichaelBarney/27bb6f629cfaed3ba3d02cc533124fb1 to your computer and use it in GitHub Desktop.
A google AdMob Interstitial implementation in SwiftUI
import SwiftUI
import GoogleMobileAds
import UIKit
final class Interstitial:NSObject, GADInterstitialDelegate{
var interstitial:GADInterstitial = GADInterstitial(adUnitID: interstitialID)
override init() {
super.init()
LoadInterstitial()
}
func LoadInterstitial(){
let req = GADRequest()
self.interstitial.load(req)
self.interstitial.delegate = self
}
func showAd(){
if self.interstitial.isReady{
let root = UIApplication.shared.windows.first?.rootViewController
self.interstitial.present(fromRootViewController: root!)
}
else{
print("Not Ready")
}
}
func interstitialDidDismissScreen(_ ad: GADInterstitial) {
self.interstitial = GADInterstitial(adUnitID: interstitialID)
LoadInterstitial()
}
}
struct ContentView:View{
var interstitial:Interstitial
init(){
self.interstitial = Interstitial()
}
var body : some View{
Button(action: {self.interstitial.showAd()}){
Text("My Button")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment