i have two view controllers imageViewController and InfoViewController. i want my ad to show when the user click on back button from InfoViewController
class imageViewController: UIViewController , GADInterstitialDelegate {
var interstitial: GADInterstitial!
override func viewDidLoad() {
self.interstitial = self.createAndLoadInterstitial()
super.viewDidLoad()
}
func createAndLoadInterstitial() -> GADInterstitial {
let interstitial: GADInterstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
interstitial.delegate = self
let request = GADRequest()
interstitial.loadRequest(request)
print("req sent")
return interstitial
}
func interstitialDidReceiveAd(ad: GADInterstitial!) {
if (interstitial.isReady) {
print("ad ready")
interstitial.presentFromRootViewController(self)
}}
func interstitialDidDismissScreen(interstitial: GADInterstitial) {
}
How should i call it with back button click from infoViewController to ImageViewCroller. i also failed to solve this problem with global variable since i am new to iOS
I would recommend to call it in one of the UIViewController
's methods. For example:
isMovingFromParentViewController()
See documentation.
EDIT:
Actually, if you want to have a VC as a delegate, you should call it from the parent VC, otherwise you will lose the delegate by popping it with the back button.
You can do it in a few ways. One would be to use a closure. Here, you would put your callback methods in the first VC and have a property in your second VC like so:
var onBack:()->()?
And you would need to set it's value when showing the second VC (for example in prepareForSegue
method):
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let destination = segue.destinationViewController as? InfoViewController {
destination.onBack = { ()->() in
self.createAndLoadInterstitial()
}
}
}
Then, in the second view controller do this:
override func isMovingFromParentViewController() -> Bool {
onBack?()
}
That is not tested btw:)