Cordova,为什么需要InAppBrowser插件来打开系统浏览器中的链接 [英] Cordova, why would InAppBrowser plugin be required to open links in system browser

查看:515
本文介绍了Cordova,为什么需要InAppBrowser插件来打开系统浏览器中的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Cordova应用程序,它是一个单页面的应用程序与一个单一的HTML文件。

I have a Cordova app, it is a single page application with a single HTML file.

所有链接都应在系统浏览器中打开。我不想要一个嵌入式InAppBrowser,而是真正的本地系统/外部浏览器。

All links should open in the system browser. I don't want an "embedded" InAppBrowser but really the native system / external browser.

到处都可以找到使用InAppBrowser的代码示例:

Everywhere we can find example of code using InAppBrowser with something like:

window.open('http://apache.org', '_system');

但是为什么我们需要安装InAppBrowser,即使我们甚至不打算使用嵌入式浏览器?

But why do we need to install InAppBrowser, even if we don't even plan to use an embedded browser?

有人可以真正地消除应该是WebView的行为,关于链接的目标。它不清楚它应该用一个 target = _blank ,但我没有看到任何其他的可以做,除非打开一个新的浏览器窗口。

Can someone really expain what is supposed to be the behavior of a WebView, regarding the target of a link. It is not clear what it is supposed to do with a target=_blank, but I don't see anything else it can do except opening a new browser window.

请注意,问题似乎只适用于iOS,因为Android(使用Crosswalk插件)使用 target = _blank

Notice that the problem seems to only be with iOS because with Android (with Crosswalk plugin) using target=_blank seems to always work fine and open in a new native browser window.

推荐答案

因此,我回答自己的问题,发现了。
注意,我只在 Cordova 5.1.1 上处理 iOS和Android (使用Crosswalk插件),它可能不适用于其他平台/版本。

So I'm answering my own question with what I've found out. Note I'm only dealing with iOS and Android (with Crosswalk plugin) on Cordova 5.1.1, and it may not apply to other platforms/versions.

即使您不需要嵌入式浏览器,也需要InAppBrowser插件。这使得 _system 目标可用,触发本机插件代码打开系统/外部浏览器。

Even if you don't need an embedded browser, InAppBrowser plugin is required. This makes the _system target available that triggers native plugin code to open the system/external browser.

所以看起来插件是一个2合1插件:它允许使用嵌入式浏览器+它允许安全地强制外部系统浏览器打开。

So it seems the plugin is somehow a "2 in 1" plugin: it permits to use an embedded browser + it permits to securely force the external system browser to open.

不清楚与 _blank 链接相关的默认WebView行为是什么

It is not clear what the default WebView behavior should be relative to _blank links (nor if it is standardized in any way for WebViews) but I've found no way to open the external browser on iOS without this plugin or native code.

如果像我一样,你不在乎关于嵌入式浏览器,但只想打开所有 _blank 目标到现有应用程序中的本机外部浏览器,没有太多的痛苦(特别是如果应用程序也是一个移动网站...),您可以在应用程序开头运行以下代码:

If like me you don't care about the embedded browser, but just want to open all _blank targets to the native external browser in an existing app, without too much pain (particularly if the app is also a mobile website...), you can run the following code at the beginning of your app:

function openAllLinksWithBlankTargetInSystemBrowser() {
    if ( typeof cordova === "undefined" || !cordova.InAppBrowser ) {
        throw new Error("You are trying to run this code for a non-cordova project, " +
                "or did not install the cordova InAppBrowser plugin");
    }

    // Currently (for retrocompatibility reasons) the plugin automagically wrap window.open
    // We don't want the plugin to always be run: we want to call it explicitly when needed
    // See https://issues.apache.org/jira/browse/CB-9573
    delete window.open; // scary, but it just sets back to the default window.open behavior
    var windowOpen = window.open; // Yes it is not deleted !

    // Note it does not take a target!
    var systemOpen = function(url, options) {
        // Do not use window.open becaus the InAppBrowser open will not proxy window.open
        // in the future versions of the plugin (see doc) so it is safer to call InAppBrowser.open directly
        cordova.InAppBrowser.open(url,"_system",options);
    };


    // Handle direct calls like window.open("url","_blank")
    window.open = function(url,target,options) {
        if ( target == "_blank" ) systemOpen(url,options);
        else windowOpen(url,target,options);
    };

    // Handle html links like <a href="url" target="_blank">
    // See https://issues.apache.org/jira/browse/CB-6747
    $(document).on('click', 'a[target=_blank]', function(event) {
        event.preventDefault();
        systemOpen($(this).attr('href'));
    });
}

这篇关于Cordova,为什么需要InAppBrowser插件来打开系统浏览器中的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆