Created
October 22, 2019 12:15
-
-
Save MichaelBarney/27bb6f629cfaed3ba3d02cc533124fb1 to your computer and use it in GitHub Desktop.
A google AdMob Interstitial implementation in SwiftUI
This file contains 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
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