问题
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).
}
To get from MainVC to ModalVC, I am using the following:
self.performSegueWithIdentifier("mainSegue", sender: self)
// Present Modally segue, default presentation, cross dissolve transition.
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)
}
}
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)
}
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()
}
}
Which gave me this error: Warning: Attempt to present GADInterstitialViewController: 0x7f9e01ca7a80 on testAd.MainVC: 0x7f9e02c44b50 whose view is not in the window hierarchy!
Ideally, I want for the Interstitial triggers to all stay on the ModalVC, simply because I want the Interstitial to appear before MainVC appears.
ANY help is appreciated. Been spinning my tires for over a day on this one... Whew... THANKS in advance!
回答1:
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)
}
Here's a full list of all the interstitial delegate methods you can use: https://firebase.google.com/docs/admob/ios/ad-events
来源:https://stackoverflow.com/questions/38469656/swift-xcode-admob-interstitial-during-dismissviewcontroller