在网络视图中打开 url - phonegap
Open url in webview - phonegap
我想知道如何在嵌入 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
您可能需要将以下内容添加到您的 phonegap xml 文件中:
<?xml version="1.0" encoding="UTF-8"?>
<phonegap>
<access origin="https://abcx.com" subdomains="true" />
</phonegap>
在 phonegap 应用程序的系统浏览器中打开页面的一种非常简单的方法是在 iframe 中呈现该页面。
<iframe src="http://www.google.com></iframe>
您可以使用 dom 更新更改 iframe 中的 URL。
这将在本机系统浏览器的页面中加载。
您必须在 config.xml 上添加此行以允许导航到外部 urls
<allow-navigation href="*" />
这将允许导航到任何外部 url,如果您只想允许导航到 google 然后添加此行
<allow-navigation href="https://google.com" />
您可以查看文档的其余部分on the plugin page
尝试:
window.open('https://google.com', '_self ', 'location=yes');
而不是:
window.location.href = 'https://google.com';
这将使用 InAppBrowser,并使用 _self 作为目标。
对于那些在使用 Phonegap 6.3.1 时遇到此问题的用户,您应该正确地将 url 列入白名单并使用 cordova-plugin-inappbrowser plugin.
继续阅读以了解如何执行此操作。
首先,确保您已将要打开的 url 列入白名单。您可以通过将它们添加到项目根目录下 config.xml 文件中 <access>
标签、<allow-intent>
标签和 allow-navigation
标签的 hrefs 来实现。有些谎言:
<?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/path。在生产中,你可能想要限制某些urls/paths)
接下来,在您的 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 插件的文档(link 以上)。
最后,要在适当的模拟器中测试应用程序(假设您安装了 Phonegap CLI),运行 以下命令:
phonegap run ios --verbose --stack-trace
phonegap run android --verbose --stack-trace
通过在项目目录中键入这些命令来安装以下插件
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>
我想知道如何在嵌入 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
您可能需要将以下内容添加到您的 phonegap xml 文件中:
<?xml version="1.0" encoding="UTF-8"?>
<phonegap>
<access origin="https://abcx.com" subdomains="true" />
</phonegap>
在 phonegap 应用程序的系统浏览器中打开页面的一种非常简单的方法是在 iframe 中呈现该页面。
<iframe src="http://www.google.com></iframe>
您可以使用 dom 更新更改 iframe 中的 URL。
这将在本机系统浏览器的页面中加载。
您必须在 config.xml 上添加此行以允许导航到外部 urls
<allow-navigation href="*" />
这将允许导航到任何外部 url,如果您只想允许导航到 google 然后添加此行
<allow-navigation href="https://google.com" />
您可以查看文档的其余部分on the plugin page
尝试:
window.open('https://google.com', '_self ', 'location=yes');
而不是:
window.location.href = 'https://google.com';
这将使用 InAppBrowser,并使用 _self 作为目标。
对于那些在使用 Phonegap 6.3.1 时遇到此问题的用户,您应该正确地将 url 列入白名单并使用 cordova-plugin-inappbrowser plugin.
继续阅读以了解如何执行此操作。
首先,确保您已将要打开的 url 列入白名单。您可以通过将它们添加到项目根目录下 config.xml 文件中 <access>
标签、<allow-intent>
标签和 allow-navigation
标签的 hrefs 来实现。有些谎言:
<?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/path。在生产中,你可能想要限制某些urls/paths)
接下来,在您的 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 插件的文档(link 以上)。
最后,要在适当的模拟器中测试应用程序(假设您安装了 Phonegap CLI),运行 以下命令:
phonegap run ios --verbose --stack-trace
phonegap run android --verbose --stack-trace
通过在项目目录中键入这些命令来安装以下插件
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>