Android 在webview中打开url-phonegap
我想知道如何在embed webview的应用程序上下文中打开url。目前,这个演示将在外部浏览器中打开一个新选项卡,所以,这不是我所期望的。我使用google.com只是为了测试 总结,我正在寻找一个功能演示Android 在webview中打开url-phonegap,android,cordova,webview,phonegap-plugins,phonegap-build,Android,Cordova,Webview,Phonegap Plugins,Phonegap Build,我想知道如何在embed webview的应用程序上下文中打开url。目前,这个演示将在外部浏览器中打开一个新选项卡,所以,这不是我所期望的。我使用google.com只是为了测试 总结,我正在寻找一个功能演示 <?xml version="1.0" encoding="UTF-8"?> <!-- config.xml reference: https://build.phonegap.com/docs/config-xml --> <widget xmlns
<?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>
您可能需要将以下内容添加到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>
您必须在config.xml上添加这一行,以允许导航到外部URL
<allow-navigation href="*" />
试试:
window.open('https://google.com', '_self ', 'location=yes');
而不是:
window.location.href = 'https://google.com';
这将使用InApp浏览器,并使用_self作为目标。对于那些在使用Phonegap 6.3.1时出现此问题的用户,您应该正确地将URL列为白名单并使用
请继续阅读,了解如何做到这一点
首先,确保已将要打开的URL列为白名单。您可以通过将它们添加到项目根目录下config.xml文件中的
标记、
标记和允许导航
标记的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>
通过在项目目录中键入以下命令来安装以下插件
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>
*{
边际:0px;
填充:0px;
}正文{宽度:100%;高度:100%;边距:0;溢出:隐藏;背景-
颜色:#252525;}
#我的框架{
边界:0px;
高度:100vh;
宽度:100%;
}
项目名称
1。您拥有的代码永远不会工作。2.如果您正在打开一个外部URL,以便您的应用程序可以作为网站包装,则应用程序商店可能会拒绝您的应用程序。Cordova/Phonegap的一个常见错误是@jcesarmobile,目前的问题是SO。Windows.openwithinappbrowser在android中运行良好,您的答案也可以。然而,这两个都在ios中失败了。在iphone中,我得到了白色页面,什么时候应该打开webview。谢谢。显然适用于android,但不适用于IOS。@user,我每天在IOS上都使用它。)您能确认IOS版本吗?从live cordova生产代码窗口复制。打开(url,“_blank”,“location=yes”);如果安装了inappbrowser插件,则工作正常。您是否已将inappbrowser插件添加到iOS?
<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>
phonegap run ios --verbose --stack-trace
phonegap run android --verbose --stack-trace
phonegap plugin add cordova-plugin-whitelist
phonegap prepare
<!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>