【问题标题】:Present Interstitial Ad every time the View Controller is presented - Swift 4每次呈现视图控制器时呈现插页式广告 - Swift 4
【发布时间】:2019-01-26 17:25:54
【问题描述】:

我只是希望每次出现某个视图控制器时都会出现一个插页式广告。这是相关代码(不是全部)...

//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
}

那么为什么这不起作用?我可以通过按一个按钮来显示广告(我显然稍微改变了代码来做到这一点),但我希望广告简单地出现在视图控制器上的演示文稿中。我错过了什么?

【问题讨论】:

    标签: swift admob interstitial viewwillappear


    【解决方案1】:

    我注意到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)
        }
    }
    

    【讨论】:

    • 感谢您的详尽回答。但是,我想知道为什么shouldDisplayAd 在private func displayAd() 中设置为false
    • 在本例中,我将其设置为false,因为我们向用户呈现ad。当ad 被解除时,将加载一个新的ad 并且isAdReady 将是true。但是,您不想仅仅根据准备好的时间来显示ad。您需要根据应用的要求确定何时显示ad 是合适的。
    • @swiftcoder 我在一个示例项目中运行了代码,没有遇到任何崩溃。如果您认为答案不完整或不可接受,最好not 将答案标记为已接受。不要觉得有义务接受我的回答。
    • 我最初忽略了需要将 Storyboard ID 更改为 buttonViewController 的事实,这就是它崩溃的原因。它不再崩溃了。但是每当我关闭广告时,另一个广告会自动重新出现,而不仅仅是显示 VC。这导致了一个无限循环,不允许我看到没有广告的 VC。这发生在你身上吗?如果是这样,您知道解决方法吗?无论哪种方式,您的答案都应该对其他人有所帮助,我会将其保留为已接受的答案。谢谢。
    • 我还注意到,当广告关闭时,VC 会动画并出现在自身之上。这发生在另一个广告自动出现之前。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多