Swift / XCode - 在dismissViewController期间的AdMob插页式广告

时间:2016-07-19 22:41:06

标签: ios swift admob segue interstitial

我有一个简单的应用程序,需要一些帮助才能正确启动插页式广告。该应用程序只有两个视图。 MainVC和ModalVC。在ModalVC上,有一个[CLOSE]按钮可以将用户返回到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次转换。只是一个FYI。对我的主要问题并不十分重要。)正在发生的是非页内加载,然后立即关闭。请参阅以下代码:

@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方法......偶然产生与上述代码相同的结果:

@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上触发了以下内容:

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

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

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

感谢任何帮助。在这一天上我的轮胎旋转了一天......哇...提前感谢!

1 个答案:

答案 0 :(得分:1)

终于弄明白了。这里的技巧是只需使用视图控制器中的按钮来触发插页式广告。然后在与插页式广告相同的视图上,使用GADInterstitialDelegate方法,特别是:interstitialWillDismissScreen

sa = { first_key: { key_nested: 'value' } }.to_shash
# => #<Shash:0x000000099d0018 @hash={:first_key=>#<Shash:0x000000099cffa0 @hash={:key_nested=>"value"}>}> 
sa.first_key.key_nested
# => "value" 

以下是您可以使用的所有插页式委托​​方法的完整列表:https://firebase.google.com/docs/admob/ios/ad-events