Skip to content

Instantly share code, notes, and snippets.

@iannase
Last active June 8, 2017 22:54
Show Gist options
  • Save iannase/ffdc863fb5acacd2c1530c92f8b289ae to your computer and use it in GitHub Desktop.
Save iannase/ffdc863fb5acacd2c1530c92f8b289ae to your computer and use it in GitHub Desktop.
Google AdMob - all code needed to get an interstitial ad working in an app
import GoogleMobileAds
class FirstViewController: UIViewController, UITextFieldDelegate, GADInterstitialDelegate {
// Your ad as a variable
var interstitialAd: GADInterstitial?
// Creates and loads an interstitial
func createAndLoadInterstitial() -> GADInterstitial {
let request = GADRequest()
let interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/1033173712") // interstitial test
interstitial.delegate = self
interstitial.load(request)
return interstitial
}
// Lets you know when it's ready
func interstitialDidReceiveAd(_ ad: GADInterstitial) {
if interstitialAd!.isReady {
interstitialAd!.present(fromRootViewController: self)
} else {
print("Ad wasn't ready")
}
print("interstitialDidReceiveAd")
}
// The ad with a 1 second delay, insert this code in whatever action you want. Delay is not required.
let when = DispatchTime.now() + 1 // change 1 to desired number of seconds
DispatchQueue.main.asyncAfter(deadline: when) {
self.interstitialAd = self.createAndLoadInterstitial()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment