简体   繁体   English

Cordova/Phonegap 在 Cordova 主 web 视图中加载外部站点

[英]Cordova/Phonegap load external site in main Cordova webview

What i want我想要的是

I have external web-resource that i want cordova webview to simple open and be able to navigate through its inner navigation.我有外部网络资源,我希望科尔多瓦 webview 能够简单地打开并能够通过其内部导航进行导航。

Description描述

InAppBrowser docs said InAppBrowser 文档说

The InAppBrowser window behaves like a standard web browser, and can't access Cordova APIs. InAppBrowser 窗口的行为类似于标准 Web 浏览器,无法访问 Cordova API。 For this reason, the InAppBrowser is recommended if you need to load third-party (untrusted) content, instead of loading that into the main Cordova webview因此,如果您需要加载第三方(不受信任)内容,而不是将其加载到 Cordova 主Web 视图中,建议使用 InAppBrowser

So i have an trusted external links (let's say http://www.google.com i want to load in main webview and want to control its process though window.location.href or user links click.所以我有一个受信任的外部链接(假设http://www.google.com我想在主 webview 中加载并希望通过 window.location.href 或用户链接单击来控制其进程。

What i tried我试过的

Generated hello world app with latest cordova and inserted使用最新的科尔多瓦生成 hello world 应用程序并插入

window.location.href = 'http://www.google.com/'

to its onDeviceReady.到它的 onDeviceReady。 It's config was already having (and i tried to change it to concrete adress)它的配置已经有了(我试图将其更改为具体地址)

<access origin="*" />

I also tried window.open and manual user link clicks, but all i've got is dialog to open link in external browser instead of rendering it inside same webview.我也尝试了 window.open 和手动用户链接点击,但我所拥有的只是在外部浏览器中打开链接的对话框,而不是在同一个 webview 中呈现它。 I also tried InAppBrowser extension, that works fine when i disable navigation bar but it has communication problems and doesn't work on manual link licks and window.location.href changes.我还尝试了 InAppBrowser 扩展,当我禁用导航栏时它工作正常,但它存在通信问题并且不适用于手动链接舔和 window.location.href 更改。

Solved this issue using following rules on both ios and android: 在ios和android上使用以下规则解决了这个问题:

<access origin="*" />
<allow-navigation href="*" />
<allow-navigation href="http://*/*" />
<allow-navigation href="https://*/*" />
<allow-navigation href="tel:*" />
<allow-navigation href="sms:*" />
<allow-navigation href="mailto:*" />
<allow-navigation href="geo:*" />

If you stuck on same problem you also need to know following: 如果你遇到同样的问题,你还需要知道以下内容:

How to load www.example.com to Cordova WebView?如何将www.example.com加载到 Cordova WebView?

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

More information:更多信息:

AFAIK by default Cordova does not allow to load external links on WebView. AFAIK 默认情况下 Cordova 不允许在 WebView 上加载外部链接。 However you can still show internal html pages which are stored in the your project (for example index.html etc).但是,您仍然可以显示存储在项目中的内部 html 页面(例如 index.html 等)。 It's a security measure.这是一种安全措施。

So you need to install this plugin cordova-plugin-whitelist and manually whitelist the domains that your app wants to load on Cordova WebView.因此,您需要安装此插件cordova-plugin-whitelist并手动将您的应用想要加载到 Cordova WebView 的域列入白名单。

If you want to allow all the links to load in WebView you can use <allow-navigation href="*" /> But it is not recommended.如果你想让所有的链接在 WebView 中加载,你可以使用<allow-navigation href="*" />但不推荐。

Relevant links:相关链接:

Official Github link 官方 Github 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM