【问题标题】:Open url in webview - phonegap在 webview 中打开 url - phonegap
【发布时间】:2016-05-07 12:35:20
【问题描述】:

我想知道如何在嵌入 webview 的应用程序上下文中打开一个 url。目前这个演示将在外部浏览器中打开一个新标签,所以,不是我所期望的。我使用 google.com 只是为了测试。

总结一下,我在找功能演示。

<?xml version="1.0" encoding="UTF-8"?>

<!-- config.xml reference: https://build.phonegap.com/docs/config-xml -->
<widget xmlns     = "http://www.w3.org/ns/widgets"
        xmlns:gap = "http://phonegap.com/ns/1.0"
        xmlns:android = "http://schemas.android.com/apk/res/android"
        id        = "com.xxx.xxxxx"
        version   = "1.0.0">

    <preference name="stay-in-webview" value="true" />

    <access origin="*" browserOnly="true" subdomains="true" />

    <content src="index.html" />

    <allow-navigation href="https://google.com/*" />

    <gap:plugin name="cordova-plugin-whitelist" source="npm" version="~1" />
    <gap:plugin name="org.apache.cordova.inappbrowser" />
    <gap:plugin name="org.apache.cordova.splashscreen" />

    <preference name="phonegap-version"           value="cli-5.4.1" />
    <preference name="permissions"                value="none"/>
    <preference name="target-device"              value="universal"/>
    <preference name="fullscreen"                 value="true"/>

</widget>

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/index.css" />
    </head>
    <body>
        <div>
            <script type="text/javascript" charset="utf-8">
                document.addEventListener("deviceready", onDeviceReady, false);

                function onDeviceReady() {
                    window.location.href = 'https://google.com';
                }
            </script>
        </div>
        <script type="text/javascript" src="cordova.js"></script>
    </body>
</html>

更新: 完整的xml文件: https://codeshare.io/Vw3Fl

【问题讨论】:

  • 1.您拥有的代码将永远无法工作。 2. 如果您打开一个外部 URL 以便您的应用程序作为网站包装器工作,您的应用程序可能会被应用商店拒绝。 Cordova/Phonegap 的一个常见错误是When designing the app, thinks phonegap works like a website or webbrowser.
  • @jcesarmobile 目前的问题是 SO。带有 InAppBrowser 的 window.open 在 android 中运行良好,您的答案也适用。然而,两者都在 ios 中失败了。在 iphone 中,我什么时候应该打开 webview 得到白页。

标签: android cordova webview phonegap-plugins phonegap-build


【解决方案1】:

试试:

window.open('https://google.com', '_self ', 'location=yes');

而不是:

window.location.href = 'https://google.com';

这将使用 InAppBrowser,并使用 _self 作为目标。

【讨论】:

  • 谢谢。显然适用于android,但不适用于IOS。
  • @user,我每天都在 iOS 上使用它))
  • 请确认IOS版本好吗?
  • 复制自实时科尔多瓦生产代码 window.open(url, '_blank', 'location=yes');如果您安装了 inappbrowser 插件,则可以完美运行。
  • 你在 iOS 中添加了 InAppBrowser 插件吗?
【解决方案2】:

您必须在 config.xml 中添加此行以允许导航到外部 url

<allow-navigation href="*" />

这将允许导航到任何外部 url,如果您只想允许导航到 google,请添加此行

<allow-navigation href="https://google.com" /> 

您可以查看文档的其余部分on the plugin page

https://github.com/apache/cordova-plugin-whitelist

【讨论】:

    【解决方案3】:

    对于那些在使用 Phonegap 6.3.1 时遇到此问题的用户,您应该将网址正确列入白名单并使用cordova-plugin-inappbrowser plugin

    继续阅读以了解如何执行此操作。


    首先,确保您已将要打开的网址列入白名单。为此,您可以在项目根目录的 config.xml 文件中将它们添加到 &lt;access&gt; 标签、&lt;allow-intent&gt; 标签和 allow-navigation 标签的 href 中。有点像:

    <?xml version='1.0' encoding='utf-8'?>
    <widget id="com.phonegap.helloworld" version="1.0.0"
            xmlns="http://www.w3.org/ns/widgets"
            xmlns:gap="http://phonegap.com/ns/1.0">
    
        ...
    
        <access origin="*" />
        <allow-intent href="*" />
        <allow-navigation href="*" />
    
        ...
    
    </widget>
    

    (注意:上述 hrefs 中的“*”允许打开任何 url/路径。在生产中,您可能希望限制为某些 url/路径)

    接下来,在您的 index.html 文件中,添加以下 javascript:

    <script type="text/javascript">
        document.addEventListener('deviceready', function() {
            var url = 'https://www.google.com' // change to whatever you want
            cordova.InAppBrowser.open(url, '_self', 'location=no');
        }, false)
    </script>
    

    此脚本使用 cordova-plugin-inappbrowser 插件,如果您使用标准 Phonegap 模板生成应用程序,该插件应该已经包含在您的 config.xml 文件中。

    脚本等待设备准备好,然后使用cordova-plugin-inappbrowser plugin 打开给定的 url。 '_self' 参数表示它在Phonegap webview 中打开页面,'location=no' 表示将没有地址栏。有关其他参数选项,请参阅 cordova-plugin-inappbrowser 插件的文档(上面的链接)。

    最后,要在适当的模拟器中测试应用程序(假设您安装了Phonegap CLI),请运行以下命令:

    phonegap run ios --verbose --stack-trace
    phonegap run android --verbose --stack-trace
    

    【讨论】:

      【解决方案4】:

      您可能需要将以下内容添加到您的 phonegap xml 文件中:

      <?xml version="1.0" encoding="UTF-8"?>
      <phonegap>
          <access origin="https://abcx.com" subdomains="true" />
      </phonegap>
      

      【讨论】:

        【解决方案5】:

        在 phonegap 应用程序的系统浏览器中打开页面的一种非常简单的方法是在 iframe 中呈现该页面。

        <iframe src="http://www.google.com></iframe>
        

        您可以使用 dom update 更改 iframe 中的 URL。

        这将在本机系统浏览器的页面中加载。

        【讨论】:

          【解决方案6】:

          通过在项目目录中输入这些命令来安装以下插件

          phonegap plugin add cordova-plugin-whitelist
          phonegap prepare
          

          然后在index.html中添加如下标签

          <!DOCTYPE html>
          <html>
          <head>
          <meta charset="utf-8" />
          <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" />
          <meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-inline' gap:; style-src 'self' 'unsafe-inline'; media-src *" />
          <style>
          *{
              margin: 0px;
              padding: 0px;
           } body {width:100%;height:100%;margin:0;overflow:hidden;background-
           color:#252525;}
           #my_iframe{
            border: 0px;
            height: 100vh;
            width: 100%;
            }
            </style>
          <title>ProjectName</title>
          </head>
          
          <body>
          <iframe src="PUT_HERE_YOUR_PROJECT_URL" id="my_iframe" frameborder="0" width="100%" height="100%" >
          </iframe>   
          </body>
          
          </html>
          

          【讨论】:

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