【问题标题】:Cordova InAppBrowser - Event callbacks not called after opening system browser ("_system")Cordova InAppBrowser - 打开系统浏览器后未调用事件回调(“_system”)
【发布时间】:2019-02-01 18:45:53
【问题描述】:

在我们的 Cordova 应用程序中,我们使用 InAppBrowser(不是吗?)来运行我们的本地 Web 应用程序。有时我们需要打开设备的浏览器(Chrome / Safari)来处理外部链接。

当我们从设备浏览器返回时,似乎 InAppBrowser 插件停止触发事件('loadstop''loadstart' 等...)。我们已尝试重新注册事件,但这似乎不起作用。

有人知道为什么吗?

这里是技术信息

我们以通常的方式打开我们的本地网络应用程序:

ref = cordova.InAppBrowser.open(url, 'blank', options);

注册事件的回调也不足为奇:

ref.addEventListener('loadstop', onLoadStop);
ref.addEventListener('loadstart', onLoadStart);
ref.addEventListener('loaderror', onLoadError);
ref.addEventListener('exit', onExit);

我们尝试使用window打开外部网络浏览器:

window.open(event.url, '_system');

或者使用第二个 InAppBrowser:

var browser = new cordova.InAppBrowser.open(event.url,'_system');

我们正在使用InAppBrowser v3.0.0。这在 Android 和 iOS 上都会发生。

【问题讨论】:

    标签: android ios cordova cordova-plugins inappbrowser


    【解决方案1】:

    显然这是known issue with InAppBrowser:打开系统浏览器会导致事件调度器发生故障。

    官方的解决方案是用新的 InAppBrowser 注册相同的事件回调。由于 InAppBrowser 执行not trigger events at all for the system browser,因此您无需担心触发双事件回调。此解决方案将事件处理恢复到旧的(本地 Web 应用)InAppBrowser。

    来自官方问题的示例:

    //example 3 : hack/solution with cordova.InAppBrowser.open(url, '_system');
    //event hander for inAppBrowser
    function inAppBrowserEventHandler(event){
      //process few logic with the event parameter
      //and if conditions met lets open it on system browser
      var url = 'https://cordova.apache.org/';
    
      //open the url in system browser
      var _inAppBrowserSystem = cordova.InAppBrowser.open(url, '_system');
    
       //for somereason, after assigning the same event handler to the _inAppBrowserSystem, the event dispatcher continues to work
      _inAppBrowserSystem.addEventListener('loadstop', inAppBrowserEventHandler);
    }
    //open the link in inside the iAB without
    var _inAppBrowser = cordova.InAppBrowser.open(url, '_blank');
    _inAppBrowser.addEventListener('loadstop', inAppBrowserEventHandler);
    

    【讨论】:

      猜你喜欢
      • 2017-06-07
      • 1970-01-01
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-17
      • 1970-01-01
      • 2019-05-16
      相关资源
      最近更新 更多