【问题标题】:Cordova InAppBrowser not workingCordova InAppBrowser 不工作
【发布时间】:2016-10-01 13:42:46
【问题描述】:

您好,我是第一次使用科尔多瓦。 我想创建一个应用程序来显示我的网站。

这是我创建应用程序的 cordova 命令:

cordova create myapp
cd myapp
cordova platform add android --save
cordova plugin add cordova-plugin-inappbrowser --save

和 index.html:

<html>
    <head>
        <!--
        Customize this policy to fit your own app's needs. For more guidance, see:
            https://github.com/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy
        Some notes:
            * gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication
            * https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly
            * Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this:
                * Enable inline JS: add 'unsafe-inline' to default-src
        -->
        <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <title>Hello World</title>
    </head>
    <body>
        <div class="app">
            <h1>Apache Cordova</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
            </div>
        </div>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript">
        document.addEventListener("deviceready", onDeviceReady, false);
        function onDeviceReady() {
            window.open = cordova.InAppBrowser.open;
            var ref = cordova.InAppBrowser.open('http://apache.org', '_self', 'location=yes');
        }
    </script>
    </body>
</html>

和 config.xml:

<?xml version='1.0' encoding='utf-8'?>
<widget id="io.cordova.hellocordova" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>HelloCordova</name>
    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>
    <author email="dev@cordova.apache.org" href="http://cordova.io">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <plugin name="cordova-plugin-whitelist" spec="1" />
    <access origin="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <platform name="android">
        <allow-intent href="market:*" />
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
    </platform>
    <engine name="android" spec="~5.2.2" />
    <plugin name="cordova-plugin-inappbrowser" spec="~1.5.0" />
</widget>

所以我没有错误地构建我的应用程序,但是当我运行时,我的应用程序中除了设备准备就绪之外什么都没有显示

【问题讨论】:

  • 你添加了Cordova白名单插件吗?
  • 为什么把句子放到"ref" var中?尝试删除它,然后放入 cordova.InAppBrowser.open('apache.org', '_self', 'location=yes');
  • 有什么错误要分享吗?
  • @甘地没有。它从一开始就在那里。
  • @Blotark 你也可以在 body onload 函数上调用 ondeviceready 函数。我忽略了你的代码。

标签: android cordova phonegap-plugins


【解决方案1】:

我有一个解决方案,我将您的“OnDeviceready”代码放入在 index.js 文件中创建的函数中,它可以工作。 因此,请尝试使用以下代码

在 index.html 中

<body>
        <div class="app">
            <h1>Apache Cordova</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
            </div>
        </div>        
        <script type="text/javascript" src="js/index.js"></script>

        <script type="text/javascript" src="cordova.js"></script>

    </body>

并在 index.js 文件中搜索定义“OnDeviceReady”函数的部分并将其替换为以下内容

onDeviceReady: function() {
        app.receivedEvent('deviceready');
          window.open = cordova.InAppBrowser.open;
            cordova.InAppBrowser.open('http://apache.org', '_self', 'location=yes');
    },

希望它能正常工作,如果您使用的是模拟器或 >4.4 版本的 Android 设备,您可以在浏览器中使用 Chrome 的检查器 (chrome://inspect/#devices) 对其进行测试和调试,以查看控制台的错误和日志.

【讨论】:

    【解决方案2】:

    权限

    安卓/

    如果没有文件夹,创建, 复制 config.xml 并将其粘贴到 xml 文件夹中,添加权限

    app/res/xml/config.xml

    <plugin name="InAppBrowser" value="org.apache.cordova.InAppBrowser" />
    

    iOS

    config.xml

    <plugin name="InAppBrowser" value="CDVInAppBrowser" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-23
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多