简体   繁体   中英

ios:how to add google ads on back button of navigation item in swift?

 let backButton = UIBarButtonItem(
        title: "",
        style: UIBarButtonItemStyle.Plain,
        target: nil,
        action: nil
    );
    self.navigationController!.navigationBar.tintColor = UIColor.whiteColor()
    self.navigationController!.navigationBar.topItem!.backBarButtonItem = backButton;

i have use above code and i want to add google Interstitial ads in back button of navigation item. my requirement is when i press back from second viewcontroller ads. should would be come and when i close ads. first view controller should open.

thanks

While creating back button, you should define selector/action methods for that backbutton.

let backButton = UIBarButtonItem(
    title: "",
    style: UIBarButtonItemStyle.Plain,
    target: self,
    action: #selector(backButtonPressed)
)

in same controller you can have method which will execute after clicking back button

func backButtonPressed(){
   Print("Back Button Pressed")

   //Write code for showing google ads here.

}

i solved my problem by adding this below code in secondViewController. and its work perfectly.

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

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