1

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

Sajjad Aims
  • 99
  • 3
  • 12

5 Answers5

5

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) oninterstitialDidReceiveAd` is not the best way to call it since you don't have control when it will show.

Community
  • 1
  • 1
r_19
  • 1,858
  • 1
  • 20
  • 16
1

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
        }

    }
Sajjad Aims
  • 99
  • 3
  • 12
0

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

konrad.bajtyngier
  • 1,786
  • 12
  • 13
  • Thanks Konrad for your replay. i call self.interstitial = self.createAndLoadInterstitial() while creating an exit button in infoVC but still i faced the same problem can u direct me more specifically ? – Sajjad Aims Feb 22 '16 at 10:12
  • Ok, so that probably doesn't work because by pressing the Back button, you are popping your VC and at the same time the delegate for your interstitial. So either you have a globally accessible class where you handle interstitials, or you call it from the parent VC. Let me edit my answer. – konrad.bajtyngier Feb 22 '16 at 10:17
  • i get the Error " Class infoViewController" has no initializer" – Sajjad Aims Feb 23 '16 at 06:31
  • You are probably using a not optional property and you don't initialize it. Hard to say which one without seeing the implementation. – konrad.bajtyngier Feb 23 '16 at 06:39
0

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{
    }
}
hardik
  • 199
  • 1
  • 6
  • 21
0

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")
            }
        }
    }``
Mr. Disability
  • 799
  • 1
  • 10
  • 19