swift - 关闭插页式广告后呈现新的 ViewController - Swift 4

标签 swift uiviewcontroller admob segue interstitial

一旦用户关闭插页式广告,我就会尝试让此按钮转到新的 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 对象具有一个委托(delegate) (GADInterstitialDelegate) 协议(protocol),您可以遵守该协议(protocol),以便在广告被关闭时收到通知。有多种处理广告状态的委托(delegate)方法,您可以在文档中查看这些方法。

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

关于swift - 关闭插页式广告后呈现新的 ViewController - Swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54373942/

相关文章:

ios - XCUITest 无法检测到我动态生成的 tableView 的可访问性 ID

ios - 重新单击按钮后程序不会更新

ios - 如何在 Storyboard 中显示弹出窗口的明确大小

iOS UIBarButtonItem 链接到 ViewController

firebase - 将 Admob 链接到 Firebase

ios - 触摸 GADBannerView subview 时 UIScrollView 不滚动

ios - 如何创建一个圆形的 UIImageView

ios - 红蜘蛛委托(delegate)未被召集

ios - 容器 View Controller addSubview 异常

android - 如何使用 intent 预加载 AdMob 插页式广告并发送到另一个 android Activity