在任何viewController中,添加这个函数。
override func viewDidLoad() {
super.viewDidLoad()
let App = UIApplication.sharedApplication().delegate as! AppDelegate
App.gViewController = self;
App.showAdmobInterstitial()
// Try below in iOS 14+ and comment above line
// var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: App, selector: Selector("showAdmobInterstitial"), userInfo: nil, repeats: false)
}
// 在AppDelegate中,声明gViewController为成员变量
var gViewController: UIViewController?
var mInterstitial: GADInterstitial!
在 AppDelegate 类中添加这些函数
func showAdmobInterstitial()
{
let kGoogleFullScreenAppUnitID = "ca-app-pub-***";
self.mInterstitial = GADInterstitial.init(adUnitID:kGoogleFullScreenAppUnitID )
mInterstitial.delegate = self
let Request = GADRequest()
Request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
mInterstitial.loadRequest(Request)
}
func interstitialDidReceiveAd(ad: GADInterstitial!)
{
ad.presentFromRootViewController(self.gViewController)
}
确保您在 AppDelegate 中添加了 GADInterstitialDelegate。
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GADInterstitialDelegate {