简体   繁体   中英

show Admob interstitial when with nav back button press iOS Swift

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

Simplest way for me to detect ios back pressed and show admob interstitial ad on backPressed is by using willMoveToParentViewController or didMoveToParentViewController in my childViewController, in your case InfoViewController .

I create my interstitial admob interstitial instance on appDelegate. So, I can use it globally and only use one instance/shared instance (i think 😀)

then just call it on willMoveToParentViewController or didMoveToParentViewController like this. Both are almost the same. ( willMoveToParentViewController will just be called ahead (my observation: ad will show while transitioning back) of didMoveToParentViewController (my observation: ad will show when fully transitioned back ))

override func willMoveToParentViewController(parent: UIViewController?) {
    super.willMoveToParentViewController(parent)
    if parent == nil {
        appDelegate.showInterstitialAd()
    }
}

or

override func didMoveToParentViewController(parent: UIViewController?) {
    super.didMoveToParentViewController(parent)
    if parent == nil {
        appDelegate.showInterstitialAd()
    }

}

Got the idea from this answer. https://stackoverflow.com/a/17551952

PS. calling interstitial.presentFromRootViewController(self) on interstitialDidReceiveAd` is not the best way to call it since you don't have control when it will show.

it will show ad just on back Press

var interstitial: GADInterstitial!
    var showAd = true
viewdidload {
self.createandLoadInterstitial()
}
 func createAndLoadInterstitial()  {
         interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
        interstitial.delegate = self
        let request = GADRequest()
        interstitial.loadRequest(request)

    } 
override func viewDidAppear(animated: Bool) {
        if (interstitial.isReady && showAd) {
            showAd = false
            print("iterstitalMain is ready")
            interstitial.presentFromRootViewController(self)
            self.createAndLoadInterstitial()
        }
        else{
        showAd = true
        }

    }

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:)

perfect tested code for add interstitial ads. in back button. add this code in secondviewController. no need to add anything in FirstViewcontroller.

func createAndLoadInterstitial()  {
    interstitial = GADInterstitial(adUnitID: "your id")
    interstitial.delegate = self
    let request = GADRequest()
    interstitial.loadRequest(request)

}
override func viewWillDisappear(animated: Bool) {
    if (interstitial.isReady && showAd) {
        showAd = false
        print("iterstitalMain is ready")
        interstitial.presentFromRootViewController(parentViewController!)
        self.createAndLoadInterstitial()
    }
    else{
    }
}

The updated function of the answer on top.

override func willMove(toParent parent: UIViewController?) {
        super.willMove(toParent: parent)
        if parent == nil {
            if interstitial.isReady {
              interstitial.present(fromRootViewController: self)
            } else {
              print("Ad wasn't ready")
            }
        }
    }``

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM