如何在cordova inappbrowser和关闭浏览器中检测重定向的URL

问题描述 投票:1回答:3

我正在使用cordova inappbrowser作为我的离子应用程序。我需要检测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 dev控制台中的错误:

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

cordova ionic-framework phonegap-plugins hybrid-mobile-app
3个回答
2
投票

这是一个cordova插件问题。 Issue link


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

删除旧版本:

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

安装4.20.0版本:

ionic cordova plugin add cordova-plugin-inappbrowser
npm install --save @ionic-native/[email protected]

也导入改变了

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

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

1
投票

好像你在这里指的是两个截然不同的例子。尝试将目标作为'_blank'。 Cordova文件说:

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

_blank:在InAppBrowser中打开。

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


0
投票

我有这个解决方案与cordova 8和离子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();
  });
© www.soinside.com 2019 - 2024. All rights reserved.