[英]Swift interstitial ad on viewDidLoad (AdMob)
我正在關注本教程
到目前為止,當我點擊按鈕時,我已經讓它工作了。 但是,我似乎無法讓它在 viewDidLoad 上顯示插頁式廣告。
這就是我所擁有的:
override func viewDidLoad() {
super.viewDidLoad()
self.interstitialAd = GADInterstitial(adUnitID: "ADUnitID")
let request = GADRequest()
// Requests test ads on test devices.
request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
self.interstitialAd.loadRequest(request)
self.interstitialAd = reloadInterstitialAd()
}
控制台不斷向我顯示:
2016-02-04 22:27:51.855 GoogleAdMobTutorial[3394:56415] 要在此設備上獲取測試廣告,請調用: request.testDevices = @[ kGADSimulatorID ];
當我點擊按鈕時:
@IBAction func showAd(sender: AnyObject) {
if self.interstitialAd.isReady {
self.interstitialAd.presentFromRootViewController(self)
}
}
廣告出現了。 當我關閉廣告時,我會在控制台中看到:
2016-02-04 22:29:16.661 GoogleAdMobTutorial[3394:56743] 請求錯誤:不會發送請求,因為已使用插頁式對象。
這是我關閉廣告時調用的函數:
func interstitialDidDismissScreen(ad: GADInterstitial!) {
self.interstitialAd = reloadInterstitialAd()
}
只是為了清楚起見:如何在視圖控制器加載時顯示插頁式廣告?
在任何 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 {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.