【问题标题】:cordova InAppBrowser does not work with _system paramcordova InAppBrowser 不适用于 _system 参数
【发布时间】:2019-05-16 08:46:26
【问题描述】:

我正在尝试使用 cordova InAppBrowser 打开支付页面,并且我想在移动设备上的系统浏览器中打开该页面。我也在尝试 _blank 参数,但 _blank 只是在同一窗口中打开该页面以应用。而且我还想通过 Cordova InAppBrowser 发送发布请求。这是我的代码:

    var redirect = 'https://SomeRef';

    var pageContent = '<form id="FormID" action="https://SomeOtherRefs" method="post">' +
      '<input type="hidden" name="RedirectURL" value="' + redirect + '">' +
      '<input type="hidden" name="Token" value="' + dataVar + '">' +
      '</form> <script type="text/javascript">document.getElementById("FormID").submit();</script>';
    var pageContentUrl = 'data:text/html;base64,' + btoa(pageContent);

    var browserRef = cordova.InAppBrowser.open(
      pageContentUrl,
      "_system",
      "hidden=no,location=no,clearsessioncache=yes,clearcache=yes"
    );

使用 _system 参数没有任何操作,_blank 只是在同一个窗口中打开页面到应用程序。如何在设备的系统浏览器中打开支付页面?

【问题讨论】:

  • 你检查白名单设置了吗? _blank 也会在 inappbrowser 中打开正确的 URL
  • @Gandhi 什么是白名单设置?还有关于_blank,是的,你是对的,但我想在系统浏览器中打开上述代码中的表单结果。

标签: javascript android cordova inappbrowser


【解决方案1】:

我终于在原始 InAppBrowser 存储库的 branch 中找到了解决方案。

有同样问题的朋友可以看看openExternal这个分支的功能。它允许像外部链接一样打开数据。

public String openExternal(String url) {
    try {
        // Omitting the MIME type for file: URLs causes "No Activity found to handle Intent".
        // Adding the MIME type to http: URLs causes them to not be handled by the downloader.
        Uri uri = Uri.parse(url);
        String scheme = uri.getScheme();

        Intent intent = "data".equals(scheme)
                ? Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_BROWSER)
                : new Intent(Intent.ACTION_VIEW);

        if ("file".equals(scheme)) {
            intent.setDataAndType(uri, webView.getResourceApi().getMimeType(uri));
        } else {
            intent.setData(uri);
        }

        intent.putExtra(Browser.EXTRA_APPLICATION_ID, cordova.getActivity().getPackageName());
        this.cordova.getActivity().startActivity(intent);
        return "";
        // not catching FileUriExposedException explicitly because buildtools<24 doesn't know about it
    } catch (java.lang.RuntimeException e) {
        LOG.d(LOG_TAG, "InAppBrowser: Error loading url " + url + ":" + e.toString());
        return e.toString();
    }
}

使用上述功能后,一切正常。

【讨论】:

    【解决方案2】:

    我发现你有两个问题(如果还有更多,请解释一下,以便我更新答案):

    1. 如何使用 _system 配置将数据发布到 inappbrowser。

      • 第一部分,你可以试试this answer。在此解决方案中,您将在其 html 中打开一个包含数据的页面,然后调用其提交事件(使用表单上的 post 方法)将数据发布到您想要的页面。
    2. 以及如何从打开的页面返回到打开器应用程序。

      • 第二个尝试使用深度链接。例如使用this plugin。使用深度链接,您的应用程序将接收 URL 打开的广播。这样,从打开的页面,重定向到包含指向 x 的链接的页面,并将 x 设置为指向您的应用页面的深层链接。我认为深度链接并不适用于所有移动设备,但我认为这是目前唯一的方法。

    【讨论】:

      猜你喜欢
      • 2016-10-22
      • 1970-01-01
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多