我正在使用 AdMob 实现插页式广告,但它没有显示。
这是我要实现插页式广告的类(class)。
import UIKit
import GoogleMobileAds
class SearchNew: UIViewController{
var interstitial: GADInterstitial!
override func viewDidLoad() {
super.viewDidLoad()
self.interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
let request = GADRequest()
request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
self.interstitial.loadRequest(request)
self.showAd()
}
func showAd() {
if self.interstitial.isReady {
self.interstitial.presentFromRootViewController(self)
}
}
最佳答案
为您的类(class)添加委托(delegate)函数 (GADInterstitialDelegate)。
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GADInterstitialDelegate {
var mInterstitial: GADInterstitial!
var gViewController: UIViewController?
调用 loadRequest,它会在广告准备好展示时触发 interstitialDidReceiveAd
func showAdmobInterstitial()
{
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)
}
从任何 viewController 调用 showAdmobInterstitial
let App = UIApplication.sharedApplication().delegate as! AppDelegate
App.gViewController = self;
App.showAdmobInterstitial()
关于ios - 如何使用 Swift 在 iOS 中使用 AdMob 实现插页式广告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35357479/