Swift / XCode-dismissViewController期间的AdMob插页式广告 [英] Swift / XCode - AdMob interstitial during dismissViewController

查看:97
本文介绍了Swift / XCode-dismissViewController期间的AdMob插页式广告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的应用,需要一些帮助才能正确触发插页式广告。该应用程序只是两个视图。 MainVC和ModalVC。在ModalVC上,有一个[关闭]按钮可将用户返回到MainVC:

I have a simple app that requires some help in getting Interstitials firing properly. The app is just two views. MainVC and ModalVC. On ModalVC, there's a [CLOSE] button that returns the user back to MainVC:

@IBAction func closeButtonPressed(sender: UIButton) {
    self.dismissViewControllerAnimated(true, completion: nil).
}

要从MainVC到ModalVC,我使用以下方法:

self.performSegueWithIdentifier("mainSegue", sender: self)
// Present Modally segue, default presentation, cross dissolve transition.

我遇到的主要问题是在closeButtonPressed上触发了插页式广告。我想触发非页内广告,而在后台执行dismissViewController。这样,一旦用户完成了插页式广告,他们就会回到MainVC。 (插页式广告每转换4次就会触发。仅供参考。对于我的主要问题并不十分重要。)发生的是插页式广告加载,然后立即关闭。参见下面的代码:

The main issue I am having is triggering the Interstitial on closeButtonPressed. I would like the interstitial to fire, while in the background, the dismissViewController performs. That way once the user is done with the interstitial, they will be back to MainVC. (Interstitials will fire every 4 transitions. Just an FYI. Not terribly important to my main issue.) What is happening is the Interstitial loads, then immediately closes. See code below:

@IBAction func closeButtonPressed(sender: UIButton) {
    //INTERSTITIAL TRIGGER!
    if (self.interstitial.isReady) {
        self.interstitial.presentFromRootViewController(self)
        self.interstitial = self.createAd()
        dismissViewControllerAnimated(true, completion: nil)
    }
    else {
        dismissViewControllerAnimated(true, completion: nil)
    }
}

另外,我尝试了以下GADInterstitialDelegate方法...附带地产生了与上面的代码相同的结果:

Additionally, I tried the following GADInterstitialDelegate method... Which incidentally produced the same results as the code above:

@IBAction func closeButtonPressed(sender: UIButton) {
    //INTERSTITIAL TRIGGER!
    if (self.interstitial.isReady) {
        self.interstitial.presentFromRootViewController(self)
        self.interstitial = self.createAd()

        interstitialDidReceiveAd(interstitial)
    }
    else {
        dismissViewControllerAnimated(true, completion: nil)
    }
}
/// Called when an interstitial ad request succeeded.
func interstitialDidReceiveAd(ad: GADInterstitial!) {
    print("interstitialDidReceiveAd")
    self.dismissViewControllerAnimated(true, completion: nil)
}

我还尝试了[CLOSE]按钮上的Unwind segue,它会触发MainVC上的以下内容:

I also tried an Unwind segue on the [CLOSE] button that triggers the following on MainVC:

@IBAction func unwindToMain(segue: UIStoryboardSegue) {
    // unwinded
    if (self.interstitial.isReady) {
        self.interstitial.presentFromRootViewController(self)
        self.interstitial = self.createAd()
    }
}

这给了我这个错误:
警告:尝试在testAd.MainVC上显示GADInterstitialViewController:0x7f9e01ca7a80:0x7f9e02c44b50,其视图不在窗口层次结构中!

Which gave me this error: Warning: Attempt to present GADInterstitialViewController: 0x7f9e01ca7a80 on testAd.MainVC: 0x7f9e02c44b50 whose view is not in the window hierarchy!

理想情况下,我希望插页式触发器全部保留在ModalVC上,只是因为我希望插页式触发器出现在MainVC出现之前。

Ideally, I want for the Interstitial triggers to all stay on the ModalVC, simply because I want the Interstitial to appear before MainVC appears.

任何帮助都是ap精打细算。在这个轮胎上旋转了一天多了...哇...提前谢谢!

ANY help is appreciated. Been spinning my tires for over a day on this one... Whew... THANKS in advance!

推荐答案

终于想到了出来。这里的技巧是仅使用视图控制器中的按钮触发插页式广告。然后在带有插页式广告的同一视图上,使用GADInterstitialDelegate方法,特别是:interstitialWillDismissScreen

Finally figured this out. The trick here is to JUST use the button in your view controller to trigger the interstitial. Then on the same view with the interstitial, use GADInterstitialDelegate methods, specifically: interstitialWillDismissScreen

class ModalVC: UIViewController, GADInterstitialDelegate 

    func interstitialWillDismissScreen(ad: GADInterstitial!) {
    self.dismissViewControllerAnimated(true, completion: nil)
}

以下是您可以使用的所有非页内广告代理方法的完整列表: https://firebase.google.com/docs/admob/ios/ad-events

Here's a full list of all the interstitial delegate methods you can use: https://firebase.google.com/docs/admob/ios/ad-events

这篇关于Swift / XCode-dismissViewController期间的AdMob插页式广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆