【问题标题】:Opening _blank links inside inappbrowser在 inappbrowser 中打开 _blank 链接
【发布时间】:2019-02-28 16:02:46
【问题描述】:

我们的cordova 应用程序中有“外部”锚链接,我们使用inappbrowser plugin 的_blank 目标打开它,这会在本机应用程序“上方”打开一个inappbrowser 实例。这些通常是指向我们基于 Web 的支持站点的链接。

现在,当用户在 inappbrowser 中访问我们的支持站点时,我们有指向使用标准 target="_blank" 的第三方站点的链接。但是,用户没有看到打开的 inappbrowser 的新实例,也没有看到打开的 system 浏览器实例,而是单独的 inappbrowser 实例将他们从支持站点带到第三方站点。

有什么方法可以让 target="_blank" 的 inappbrowser 行为按预期工作?

【问题讨论】:

标签: cordova inappbrowser


【解决方案1】:

由于InAppBrowser的设计,它只能有一个实例,所以你唯一的选择是在系统浏览器上打开外部链接。 您可以监听 loadstart 事件,如果 url 不包含您的域,则在系统浏览器上打开它,如下所示:

var browserRef = window.cordova.InAppBrowser.open('http://www.yourdomain.com/support', '_blank');

browserRef.addEventListener('loadstart', function(event) {
    if((event.url).indexOf('www.yourdomain.com') === -1) {
        window.cordova.InAppBrowser.open(event.url, '_system');
    }
});

但我认为这不会避免在 inAppBrowser 窗口上加载外部站点,因此您还必须注入 javascript 才能返回到 inAppBrowser 上的上一页

browserRef.addEventListener('loadstop', function(event) {
    browserRef.executeScript({ code: 'window.history.back();' }, null);
});

【讨论】:

    【解决方案2】:

    jcesarmobile 的回答只是部分有效。

    当您打开 _system InAppBrowser 时,对 browserRef 的引用以及映射到“loadstart”和“loadstop”的事件都会丢失。这是很久以前的 InAppBrowser 的一个非常糟糕的行为。不知道他们是否打算修复它。我有submitted an issue 以防万一。

    换句话说,这仅适用于您打开的第一个外部链接。

    进一步的页面加载将不再相同。到目前为止,我发现的非常不雅的解决方案是在启动“_system”浏览器后重新实例化“_blank”浏览器。但为此,您必须存储导航历史记录才能使用之前的 URL 启动新的 InAppBrowser。

    下面的代码对我有用,但有时新的 InAppBrowser 没有显示,我正在调查原因。

    库: “离子原生”:“2.2.11” “打字稿”:“2.0.9”

    import { Component } from '@angular/core';
    import { InAppBrowser, Splashscreen } from 'ionic-native';
    import { Platform } from 'ionic-angular';
    import 'rxjs/Rx';
    
    @Component({
      selector: 'page-home',
      templateUrl: 'home.html'
    })
    export class HomePage {
    
      browser: InAppBrowser;
      sysBrowser: InAppBrowser;
      urlHistory: string[] = [];
      url: string;
    
      constructor(public platform: Platform) {
        this.platform = platform;
        this.url = 'http://www.yourdomain.com/support';
      }
    
      ionViewDidLoad() {
        this.platform.ready().then(() => {
          this.browser = new InAppBrowser(this.url, '_blank', 'location=no,zoom=no,hidden=no');
          this.urlHistory.push(this.url);
          this.addEventsToBlankBrowser();
        });
      }
    
      addEventsToBlankBrowser() {
          this.browser.on('loadstart').subscribe(
            event => {
              if (event.url.indexOf('www.yourdomain.com') === -1) {
                let previousUrl = this.urlHistory.pop();
                this.browser.executeScript({ code: 'window.location.href = "' + previousUrl + '";' });
                this.sysBrowser = new InAppBrowser(event.url, '_system');
                this.url = previousUrl;
                this.urlHistory.push(previousUrl);
                this.browser = new InAppBrowser(previousUrl, '_blank', 'location=yes,zoom=no,hidden=no');
                this.browser.show();
                this.addEventsToBlankBrowser();
              } else {
                this.url = event.url;
                this.urlHistory.push(event.url);
              }
            }
          );
      }
    }

    【讨论】:

    • 您的解决方案似乎工作得几乎完美,只有两个方面需要提及: - 当用户重新进入应用程序时,主页会重新加载/重新启动,因此您需要格外小心表单值、动画和其他东西; - 每次使用时,应用程序都会在内存中扩展,因为旧的/以前的 InAppBrowser 窗口仍然存在于某个地方……但就目前而言,我认为这是最好的解决方案
    【解决方案3】:

    正如在之前的 cmets 中提到的:InAppBrowser 解决方案目前一次只支持一个 InAppBrowser 实例。 使用“@ionic-native/core”版本 4.12.2、带有角度代码的 typescript 2.6.2 示例实现如下所示:

    import { NavController, Platform, LoadingController } from 'ionic-angular';
    import { InAppBrowser } from "@ionic-native/in-app-browser";
    
    export class HomePage {
      ref;
    
      constructor(public platform:Platform, private iab: InAppBrowser) {
        platform.ready().then(() => {
          this.initApp();
        })
      }
    
        openIAB(url, target, options){
        this.ref = undefined
        this.ref = this.iab.create(url, target, options)
        this.ref.on('loadstop').subscribe((event) => {
          console.log('loadstop')      
          this.ref.show();
        });
    
        this.ref.on('exit').subscribe((event) => {
          console.log(' exit called ')
          this.platform.exitApp();      
          });      
        }
    
        openSystemWindow(){ this.openIAB('http://www.google.com','_system', {location: 'no'}) }
    
        initApp(){
          this.openIAB('http://www.google.com','_blank',{location:'no', zoom:'no',
          clearcache: 'yes', hidden: 'yes', hardwareback:'no'})
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-05-08
      • 1970-01-01
      • 2012-07-06
      • 2015-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-10
      • 2014-07-20
      相关资源
      最近更新 更多