OC code here: https://developers.google.com/admob/ios/app-open-ads
All code below goes to AppDelegate.swift
Import GoogleMobileAds:
import GoogleMobileAds
In class definition add GADFullScreenContentDelegate:
class AppDelegate: UIResponder, UIApplicationDelegate, GADFullScreenContentDelegate {
Add two variables:
var appOpenAd: GADAppOpenAd?
var loadTime = Date()
Add these three functions:
func requestAppOpenAd() {
let request = GADRequest()
GADAppOpenAd.load(withAdUnitID: "YOUR_ADUNIT_ID",
request: request,
orientation: UIInterfaceOrientation.portrait,
completionHandler: { (appOpenAdIn, _) in
self.appOpenAd = appOpenAdIn
self.appOpenAd?.fullScreenContentDelegate = self
self.loadTime = Date()
print("Ad is ready")
})
}
func tryToPresentAd() {
if let gOpenAd = self.appOpenAd, let rwc = self.window?.rootViewController, wasLoadTimeLessThanNHoursAgo(thresholdN: 4) {
gOpenAd.present(fromRootViewController: rwc)
} else {
self.requestAppOpenAd()
}
}
func wasLoadTimeLessThanNHoursAgo(thresholdN: Int) -> Bool {
let now = Date()
let timeIntervalBetweenNowAndLoadTime = now.timeIntervalSince(self.loadTime)
let secondsPerHour = 3600.0
let intervalInHours = timeIntervalBetweenNowAndLoadTime / secondsPerHour
return intervalInHours < Double(thresholdN)
}
Finally call tryToPresentAd() from applicationDidBecomeActive():
func applicationDidBecomeActive(_ application: UIApplication) {
self.tryToPresentAd()
}
That's it.
Edit: Added the need to import GoogleMobileAds