Ionic 2 cordova InAppBrowser browser.close() is not a function

我与影子孤独终老i 提交于 2020-04-30 04:44:22

问题


I am integrating a payment gateway in my ionic app in which i am using node js on the server side. On a click of button i am redirecting user to the payment gateway page using ionic native inAppBrowser after which i am listening to url events. When the payment is successful then the browser will redirect to success url else it will redirect to failed url. Based on this i am handling the UI in the app. My problem is when i listen to the success/failed url and call the close browser event i get this error.

Uncaught TypeError: this.browser.close is not a function

I have also tried hide() method but i get the same error. Following is my code.

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Http } from '@angular/http';
import { InAppBrowser, InAppBrowserEvent } from '@ionic-native/in-app-browser';
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  event : any;
  browser:any;
  failed : boolean = false;
  success : boolean = false;
  constructor(public navCtrl: NavController, private http: Http,private iab: InAppBrowser) {

  }

  payment(){
    this.browser = this.iab.create('http://192.168.0.59:3000/post').on("loadstop")
     .subscribe((ev: InAppBrowserEvent) => {

              if(ev.url == "http://192.168.0.59:3000/failure"){
                console.log("payment failed");
                this.failed = true;
                this.closeBrowser();
              }else if(ev.url == "http://192.168.0.59:3000/success"){
                console.log("payment success");
                this.success = true;
              }

          });

      }

      closeBrowser(){
        this.browser.close();
      }

    }

I am also testing the app on a real device so there isn't any cordova or platform issue...Please tell me what mistake i am making. A proper description with the answer will be quite helpful for my future references.


回答1:


Try adding following line inside constructor:

this.browser = this.iab.create('http://192.168.0.59:3000/post');

You can also use const for that purpose like:

const browser = this.iab.create('http://192.168.0.59:3000/post');

Then, in payment method just do

this.browser.on("loadstop")
.subscribe((ev: InAppBrowserEvent) => {
    // check conditions according to logic
});

You can simply use browser.on() if using const browser in constructor. And then in closeBrowser() method, this.browser.close(); will work.

Check Official Documentation



来源:https://stackoverflow.com/questions/47468086/ionic-2-cordova-inappbrowser-browser-close-is-not-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!