如何在Cordova Inappbrowser中检测重定向的URL并关闭浏览器

时间:2019-02-13 12:38:10

标签: cordova ionic-framework phonegap-plugins hybrid-mobile-app

我正在为我的离子应用程序使用cordova内置浏览器。我需要检测url何时重定向到http://xx.success.html or http://xx.failure.html

我的代码:

public openWithCordovaBrowser(url: string) {
    //where are events triggered (_self,_blank,_window)?
    let target = "_self"; 

    let inAppBrowserObject = this.inAppBrowser.create(url, target, this.inAppBrowserOptions);

                //When the page is success, close inappbrowser
    if (inAppBrowserObject.on('loadstart').subscribe) {
      inAppBrowserObject.on('loadstart').subscribe((e: InAppBrowserEvent) => {
        if (e && e.url) {
          if (e.url.includes('success.html')) {
            inAppBrowserObject.close(); //not reached?
          }
        }
      });
    }

               //When the InAppBrowser is closed, check and use the last viewed URL
    if (inAppBrowserObject.on('exit').subscribe) {
      inAppBrowserObject.on('exit').subscribe((e: InAppBrowserEvent) => {
        if (url) {
        console.log('exit: ' + e); //this is reached
        }
      });
    }
  }

无效,并且inAppBrowserObject.close()从未被调用。

chrome开发人员控制台中的

错误:

Error in Success callbackId: InAppBrowser780208391 : TypeError: Object(...) is not a function
cordova.js:310 TypeError: Object(...) is not a function
    at InAppBrowserObject.close (vendor.js:81063)
    at SafeSubscriber._next (main.js:411)
    at SafeSubscriber.__tryOrUnsub (vendor.js:20899)
    at SafeSubscriber.next (vendor.js:20846)
    at Subscriber._next (vendor.js:20786)
    at Subscriber.next (vendor.js:20750)
    at Channel.fire (cordova.js:843)
    at InAppBrowser._eventHandler (inappbrowser.js:48)
    at cb (inappbrowser.js:108)
    at Object.callbackFromNative (cordova.js:291)

但是关闭浏览器会调用console.log('exit: ' + e);

我的inAppBrowser对象如下所示: enter image description here

3 个答案:

答案 0 :(得分:2)

这是Cordova插件的问题。 bs4 documentation


我删除了cordova和npm插件5.x.x并安装了4.20.0。那解决了问题。

删除旧版本:

thresh = 0.5  # or whatever you want
(10 * df.drop(df.columns[i], axis=1)).lt(df.iloc[:,i], axis=0).sum(1) / (df.shape[1] - 1) > thresh

0     False
1      True
2      True
3     False
4     False
5     False
6     False
7     False
8     False
9     False
10    False
dtype: bool

安装4.20.0版本:

cordova plugin remove cordova-plugin-inappbrowser
npm uninstall @ionic-native/in-app-browser --save

也从

导入
ionic cordova plugin add cordova-plugin-inappbrowser
npm install --save @ionic-native/in-app-browser@4.20.0

import { InAppBrowser, InAppBrowserObject } from '@ionic-native/in-app-browser/ngx'

答案 1 :(得分:1)

似乎您在这里引用了两个不同的实例。尝试将目标设置为“ _blank”。科尔多瓦文档说:

_self::如果URL在白名单中,则在Cordova WebView中打开;否则,在InAppBrowser中打开。

_blank::在InAppBrowser中打开。

参考:https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-inappbrowser/

答案 2 :(得分:0)

我有此解决方案与cordova 8和ionic 3.20一起很好地工作。您需要调用.show吗?似乎与屏幕从未处于.show状态有关-也许吗?如果您的浏览器默认为隐藏。

  const iabrowser = this.inAppBrowser.create(this.signupUrl,'_self',iabOptions);

  this.loader.presentLoading();
    iabrowser.on('loadstart').subscribe(event =>{
    console.log(event);
    iabrowser.executeScript({code: "alert('loadstart! I am an alert box!!')"});
    if (event && event.url) {
        if (event.url.includes('/app/main/login')) {
            iabrowser.close();
        }
    }
    iabrowser.show();
  });
  iabrowser.on('loadstop').subscribe(event =>{
    console.log(event);
    this.loader.dismissLoading();
  });