在插页式广告关闭后显示新的 ViewController - Swift 4

Present new ViewController after Interstitial Ad is closed - Swift 4

我试图在用户关闭插页式广告后将此按钮切换到新的 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)
}

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 广告文档:Interstitial Ads