viewDidLoad(AdMob)上的Swift插页式广告 [英] Swift interstitial ad on viewDidLoad (AdMob)
问题描述
我正在关注教程
到目前为止,当我点击按钮时,我可以使用它。但是我似乎无法在viewDidLoad上显示插页式广告。
So far I have got it to work when I click the button. However I cannot seem to get it to show an interstitial ad on 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];
2016-02-04 22:27:51.855 GoogleAdMobTutorial[3394:56415] To get test ads on this device, call: request.testDevices = @[ kGADSimulatorID ];
当我点击按钮时:
@IBAction func showAd(sender: AnyObject) {
if self.interstitialAd.isReady {
self.interstitialAd.presentFromRootViewController(self)
}
}
广告出现。当我关闭广告时,我会在控制台中看到这个:
The ad shows up. When I dismiss the ad, I get this in the console:
2016-02-04 22:29:16.661 GoogleAdMobTutorial [3394:56743 ]请求错误:由于使用了插页式对象,因此无法发送请求。
2016-02-04 22:29:16.661 GoogleAdMobTutorial[3394:56743] Request Error: Will not send request because interstitial object has been used.
这是我关闭广告时调用的函数:
This is the function that is called when I dismiss the ad:
func interstitialDidDismissScreen(ad: GADInterstitial!) {
self.interstitialAd = reloadInterstitialAd()
}
问题
为了清楚起见:如何在viewcontroller加载时是否会显示插页式广告?
Question
Just for clarity: How do I get an interstitial ad to show when the viewcontroller loads?
推荐答案
在任何viewController中,添加此功能。
In any viewController, add this function.
override func viewDidLoad() {
super.viewDidLoad()
let App = UIApplication.sharedApplication().delegate as! AppDelegate
App.gViewController = self;
App.showAdmobInterstitial()
}
//在AppDelegate中,声明gViewController作为成员变量
// In AppDelegate, declare gViewController as member variable
var gViewController: UIViewController?
var mInterstitial: GADInterstitial!
在AppDelegate类中添加这些函数
Add these function in AppDelegate class
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。
make sure you added GADInterstitialDelegate in AppDelegate.
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GADInterstitialDelegate {
这篇关于viewDidLoad(AdMob)上的Swift插页式广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!