0
votes

I am new to ionic framework and I am unable to manage hardware back click functionality in Iframe. I am using Iframe to load certain url. While clicking the hardware back button I should be able to navigate back to the browser history page. But whenever I click hardware back its exiting the app.

`<iframe #iframe id="iframe" style="height: 100%;width: 100%;" src="your url"></iframe>`
@ViewChild('iframe') iframe:ElementRef;

constructor(public platform:Platform,public nav:Nav){

  platform.registerBackButtonAction(() => {
     if(this.nav.canGoBack()){
        this.iframe.nativeElement.contentWindow.history().back();
     }
  });
}
1

1 Answers

0
votes

You can use window.history.back():

  ionViewDidLoad() {
    this.navBar.backButtonClick = (e: UIEvent) => {
      window.history.back();
    }
    this.initializeBackButtonCustomHandler();
  }

  ionViewWillLeave() {
    // Unregister the custom back button action for this page
    this.unregisterBackButtonAction && this.unregisterBackButtonAction();
  }

  initializeBackButtonCustomHandler(): void {
    this.unregisterBackButtonAction = this.platform.registerBackButtonAction(function(event){
      window.history.back();

    }, 101); // Priority 101 will override back button handling (we set in app.component.ts) as it is bigger then priority 100 configured in app.component.ts file */
  }

More info about this method can be found here.