作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我只是希望每次出现某个 View Controller 时都会出现一个插页式广告。这是相关代码(不是全部)...
//import ads, set delegate, and declare variable...
import UIKit
import GoogleMobileAds
class myVC: UIViewController, GADInterstitialDelegate {
var interstitial: GADInterstitial!
准备好广告并在 View Controller 出现时展示它......
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
}
那么为什么这不起作用呢?我可以通过按下按钮来显示广告(显然我稍微修改了代码来做到这一点),但我希望广告只是与 View Controller 上的演示文稿一起显示。我错过了什么?
最佳答案
我注意到插页式广告
的问题是,当调用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)
}
}
关于swift - 每次呈现 View Controller 时呈现插页式广告 - Swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54380825/
我是一名优秀的程序员,十分优秀!