【问题标题】:Present new ViewController after Interstitial Ad is closed - Swift 4关闭插页式广告后呈现新的 ViewController - Swift 4
【发布时间】:2019-01-25 23:02:43
【问题描述】:

一旦用户关闭插页式广告,我正在尝试将此按钮设置为新的 ViewController。 segue 代码在没有广告代码的情况下工作,而广告代码在没有 segue 代码的情况下工作。无论哪一个先发生,但我不知道如何让它们一起工作。您会注意到我尝试将self.present... 移动到不同的地方但没有成功(请参阅注释行)。

@IBAction func adButton(_ sender: UIButton) {

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let myVC = storyboard.instantiateViewController(withIdentifier: "newVC")
    //self.present(myVC, animated: true, completion: nil)

    if (interstitial.isReady) {
        interstitial.present(fromRootViewController: self)
        interstitial = createAd()
        //self.present(myVC, animated: true, completion: nil)
    }
    self.present(myVC, animated: true, completion: nil)
}

【问题讨论】:

    标签: swift uiviewcontroller admob segue interstitial


    【解决方案1】:

    interstitial 对象有一个委托 (GADInterstitialDelegate) 协议,您可以遵守该协议以便在广告被关闭时收到通知。您可以在文档中查看几种处理广告状态的委托方法。

    class ViewController:UIViewController, GADInterstitialDelegate {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
            interstitial.delegate = self
            let request = GADRequest()
            interstitial.load(request)
        }
    
        @IBAction func adButton(_ sender: UIButton) {
    
            if (interstitial.isReady) {
                interstitial.present(fromRootViewController: self)
            } else {
               self.present(myVC, animated: true, completion: nil)
           }
        }
    
        //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")
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let myVC = storyboard.instantiateViewController(withIdentifier: "newVC")
            self.present(myVC, animated: true, completion: nil)
        }
    }
    

    Google Ads 文档:Interstitial Ads

    【讨论】:

    • 我之前尝试了第二个 func 示例,但没有成功。在其中我放置了一个对新 ViewController 的 segue,但它不起作用。这些函数是需要在代码中的其他地方调用还是只需要创建?
    • 它们将成为符合GADInterstitialDelegate 协议的任何对象的一部分,通常这是呈现广告的视图控制器。
    • 我将 func interstitialDidDismissScreen ... 放在创建 Interstitial Ad 的 VC 底部,但我什至无法获得 print 语句。而且我知道我的其余代码可以正常工作,因为我可以展示 Interstitial Test Ad 或继续使用新的 VC。我仍然无法弄清楚如何做到这两点。
    • 我更新了示例代码以显示符合协议。您需要确保将视图控制器设置为您创建的广告的代理。我会尝试让广告工作的委托方法。一旦这些方法被调用,你应该能够在广告被关闭时展示你自己的视图控制器。
    • 谢谢。感谢您的跟进。我刚看到你的编辑,我会试一试。我之前应该提到的一个潜在问题是,我有四个不同的按钮,它们会导致四个不同的新 VC,具体取决于用户选择按下哪一个。根据您的回答,新 VC 只有一个选项。我应该尝试使用 switch 语句来解决这个问题还是有更有效的方法?
    猜你喜欢
    • 1970-01-01
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 2021-05-25
    • 1970-01-01
    相关资源
    最近更新 更多