Cordova/PhoneGap的InAppBrowser插件--window.open无法在默认浏览器中打开链接

3

我想在我的网页应用程序中添加InAppBrowser PhoneGap插件,以便在移动默认浏览器中打开链接。

网页应用程序使用AngularJS和IonicFramework进行编程。

我知道有非常相似的问题,但是我尝试过的解决方案都没有奏效。

因此,当我创建apk文件并在Android手机设备上运行它时,有些事情并不像预期的那样工作。顺便说一下,我是通过https://build.phonegap.com/构建apk文件的。

在我的索引文件中,我有这个函数:

    <!-- JQuery library -->
    <script src="lib/jquery-2.1.3.min.js"></script>
    <script type="text/javascript">
        $(document).on('click', '.external', function (e) {
            e.preventDefault();
            window.open(e.target.href, '_system');
        });
    </script>

我的一个问题是与这段代码相关:

  <p>
    <a id="btn-noticias" class="button button-block button-stable external" 
        ng-href="{{noticia.uri}}">
      Ver noticia en la web
    </a>
  </p>

链接在应用视图中打开,而不是默认浏览器中打开。
并且在这段代码中:
            <a class="item lista-item examen external" ng-repeat="examen in filteredExSept"
               ng-href="{{examen.URI}}"
               ng-hide="errorConexionExSept || examenesSeptiembre.length==0 || filteredExSept.length==0">
                <div class="tituloExamen" ng-bind-html="examen.asignatura"></div>
            </a>

这个应用程序甚至不能打开链接,无论是在浏览器中还是在应用程序视图中都无法打开链接。

在这里你可以看到我的config.xml文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<widget id="com.ionicframework.myapp377389" 
        version="0.0.1" 
        xmlns="http://www.w3.org/ns/widgets" 
        xmlns:cdv="http://cordova.apache.org/ns/1.0"
        xmlns:gap="http://phonegap.com/ns/1.0">

    <name>SmartCULM</name>

    <description>
        An application...
    </description>

    <author email="gps_e25@googlegroups.com">
      Daniel García Páez
    </author>

    <content src="index.html"/>
    <access origin="*"/>

    <!--
        If you do not want any permissions to be added to your app, add the
        following tag to your config.xml; you will still have the INTERNET
        permission on your app, which PhoneGap requires.
    -->
    <preference name="permissions"                value="none"/>

     <!-- Core plugins -->
    <gap:plugin name="org.apache.cordova.inappbrowser" />
    <gap:plugin name="org.apache.cordova.device" />

    <!-- Define app icon for each platform. -->
    <icon src="img/apk_icon.png" />
</widget>

请问我是否有做错什么事?是使用了InAppBrowser插件还是angularjs代码存在问题?
您可以在这里查看整个项目。
更新: 我在PhoneGap文档中找到了有关config.xml中访问标签(也称为白名单)的下一条信息:
在Android上,如果一个域名被列入白名单,链接将占据整个Web视图。如果没有,它将在浏览器中打开。
因此,我理解,如果要在默认浏览器中打开所有我的链接(http://culm.unizar.es/*和https://culm.unizar.es/*),我必须避免将它们包含在白名单中。如何实现?我有点困惑……我已经尝试过,但不确定是否正确。

你能告诉我们具体是哪里出了问题吗? - Nic Raboy
正如我之前所说,在我发布的第一段代码中,应用程序在应用内打开链接,而不是在默认浏览器中打开。而在第二段代码中,该应用程序根本不会打开链接。 - daniegarcia254
看起来是重复的http://stackoverflow.com/questions/27801070/how-do-i-open-a-link-in-the-system-browser-from-content-in-an-in-app-browser-in/27803285#27803285。 - unobf
@unobf 我已经尝试了那个解决方案,但它并没有解决我的问题。 - daniegarcia254
@unobf 我已经更新了我的问题,并附上最近的研究...如果有帮助的话... - daniegarcia254
2个回答

2
这是我使用的代码,是从这里编译而来的,综合了几个来源:
    var externalLinkToOpen = $(this).attr("href");
    if (app.isPhoneGap()) {
        if (device.platform === 'Android') {
            navigator.app.loadUrl(externalLinkToOpen, { openExternal: true });
        } else {
            window.open(externalLinkToOpen, '_system');
        }
    } else {
        window.open(externalLinkToOpen, "_blank");
    }

"app.isPhoneGap"只是检查设备是否未定义,以判断是否作为网站运行(我构建的几乎所有应用都作为网站)。是的,我在项目中也安装了InApp浏览器插件。


仍然对我不起作用。我尝试了很多种替代方案......我相当确定它一定与我项目中InAppBrowser插件的错误配置有关。我确信已经使用“cordova plugin add”命令添加了它,并且它也已经添加到了“config.xml”中。您可以给我提供一个您项目配置的示例吗?当您说“是的,我在项目中也有InApp浏览器插件”时,您是如何做到的?也许我在操作上漏掉了某些东西... - daniegarcia254
顺便提一下,在“app.isPhoneGap”这个句子中,“app”和“isPhoneGap”是来自InAppBrowser的全局变量,对吧? - daniegarcia254
抱歉,App是我使用的一组常用函数的命名空间对象。isPhoneGap()是其中一个函数:App.prototype.isPhoneGap = function () { return !(device == undefined); }; - mharr
我本来就是这么想的。但是...仍然在应用视图中打开链接,而不是默认浏览器。这就是为什么我怀疑这一定与 InAppBrowser 插件的错误配置有关。 - daniegarcia254

1
在config.xml中:
<access origin="*" />
<allow-intent href="*" />
<allow-navigation href="*" />

请更新一下,如果它起作用了。我在安卓上也遇到了同样的问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接