swift - 每次呈现视图控制器时呈现插页式广告 - Swift 4
问题描述
我只是希望每次出现某个视图控制器时出现一个插页式广告。这是相关代码(不是全部)...
//import ads, set delegate, and declare variable...
import UIKit
import GoogleMobileAds
class myVC: UIViewController, GADInterstitialDelegate {
var interstitial: GADInterstitial!
准备好广告并在呈现视图控制器时呈现它......
override func viewDidLoad() {
super.viewDidLoad()
interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
let request = GADRequest()
interstitial.load(request)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if (interstitial.isReady) {
interstitial.present(fromRootViewController: self)
interstitial = createAd()
}
}
确保广告将准备好下次...
func createAd() -> GADInterstitial {
let inter = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
inter.load(GADRequest())
return inter
}
那么为什么这不起作用呢?我可以通过按一个按钮来显示广告(我显然稍微改变了代码来做到这一点),但我希望广告简单地出现在视图控制器上的演示文稿中。我错过了什么?
解决方案
我注意到的问题interstitial ad
是它在被调用时还viewWillAppear
没有准备好。viewDidAppear
因此,检查interstitial.isReady
失败。该GADInterstitialDelegate
方法已调用interstitialDidReceiveAd
,它将告诉您添加何时准备就绪。然后,您需要围绕监视广告何时准备好以及是否已经向用户展示广告来构建一些逻辑。
import UIKit
import GoogleMobileAds
class AdViewController:UIViewController, GADInterstitialDelegate {
var interstitial: GADInterstitial!
private var shouldDisplayAd = true
private var isAdReady:Bool = false {
didSet {
if isAdReady && shouldDisplayAd {
displayAd()
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
print(#function)
interstitial = GADInterstitial(adUnitID: "/6499/example/interstitial")
interstitial.delegate = self
let request = GADRequest()
interstitial.load(request)
print(#function, "shouldDisplayAd", self.shouldDisplayAd, "isAdReady", self.isAdReady)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print(#function, "shouldDisplayAd", self.shouldDisplayAd, "isAdReady", self.isAdReady)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print(#function, "shouldDisplayAd", self.shouldDisplayAd, "isAdReady", self.isAdReady)
}
@IBAction func adButton(_ sender: UIButton) {
print(#function, "shouldDisplayAd", self.shouldDisplayAd, "isAdReady", self.isAdReady)
displayAd()
}
private func displayAd() {
print(#function, "ad ready", interstitial.isReady)
if (interstitial.isReady) {
shouldDisplayAd = false
interstitial.present(fromRootViewController: self)
}
}
private func presentViewController() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myVC = storyboard.instantiateViewController(withIdentifier: "buttonViewController")
self.present(myVC, animated: true) { [unowned self] in
self.shouldDisplayAd = true
print(#function, "shouldDisplayAd", self.shouldDisplayAd, "isAdReady", self.isAdReady)
}
}
func createAndLoadInterstitial() -> GADInterstitial {
interstitial = GADInterstitial(adUnitID: "/6499/example/interstitial")
interstitial.delegate = self
interstitial.load(GADRequest())
shouldDisplayAd = false
return interstitial
}
/// Tells the delegate an ad request failed.
func interstitialDidFail(toPresentScreen ad: GADInterstitial) {
print(#function, "ad ready", interstitial.isReady)
}
func interstitialDidReceiveAd(_ ad: GADInterstitial) {
print(#function, "ad ready", interstitial.isReady)
isAdReady = true
}
//Tells the delegate the interstitial is to be animated off the screen.
func interstitialWillDismissScreen(_ ad: GADInterstitial) {
print("interstitialWillDismissScreen")
}
//Tells the delegate the interstitial had been animated off the screen.
func interstitialDidDismissScreen(_ ad: GADInterstitial) {
print("interstitialDidDismissScreen")
presentViewController()
interstitial = createAndLoadInterstitial()
print(#function, "shouldDisplayAd", shouldDisplayAd, "isAdReady", isAdReady)
}
}
推荐阅读
- c# - C# 的 PluralizationService 中是否存在错误,或者我没有正确设置某些内容?
- c++ - 从 QThread 发出的信号从未收到
- python - 如何使用 jupyter notebook 上的 pandas plot 函数绘制数据帧切片的中值和均值?
- android - Android 应用程序未通过 webview SSL Hnadler 的批准
- wpf - 如何在多行文本块的末尾嵌入视觉画笔?
- python - 我正在尝试制作一个 YT api,但每次我得到的都是这个错误:没有这样的文件或目录:'client_secrets_file.json'
- python - 在Python中正确打印for循环内的for循环
- reactjs - 如何使用反应测试库测试是否存在延迟加载的组件?
- silverstripe - SilverStripe 4 流畅:“表 'ss4_project.Member_Localised' 不存在”
- javascript - 发送邮件时的模板文字格式