Cordova Wrapper 应用程序,内部链接在应用程序中加载,外部链接在浏览器中加载

2024-01-01

我有一个简单的Cordova https://cordova.apache.org/包装器应用程序指向外部网页,而不定义任何自己的视图。

我想来自该域的所有内部链接都将加载到应用程序内, but 所有外部链接(http://twitter.com http://twitter.com等)在系统浏览器中加载,因此页面具有后退/前进功能。

在具有视图的普通应用程序中,我可以设置target='_system'在默认浏览器中加载链接,或使用Cordova-插件-inappbrowser https://www.npmjs.com/package/cordova-plugin-inappbrowser在 Web 浏览器视图中显式打开链接。不幸的是,在这种情况下,我无法编辑服务器端代码,因此需要一个在应用程序内工作的解决方案。

如果我定义config.xml因此,内部和外部链接都会加载到应用程序中。

<content src="http://example.com/" />
<access origin="*" />
<allow-navigation href="*" />

如果我定义config.xml with allow-intent,然后在系统浏览器中打开内部和外部链接。

<content src="http://example.com/" />
<access origin="*" />
<allow-navigation href="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />

Others https://stackoverflow.com/a/29430835/385730 have 建议 http://weblog.west-wind.com/posts/2015/Jul/02/External-Links-in-Cordova-for-iOS#HandlingTargetLinks使用自定义 javascript 来覆盖target to _system,但是由于我没有自己的观点,所以我不能真正这样做。

是否可以定义allow-intent为了科尔多瓦插件白名单 https://github.com/apache/cordova-plugin-whitelist以这样的方式包括所有非内部域的 URL?

或者我需要以某种方式覆盖shouldStartLoadWithRequest in MainViewController然后打电话[[UIApplication sharedApplication] openURL:url]?


好的,经过一些实验和建议Hayyaan https://stackoverflow.com/users/3846593/hayyaan-ahmad,我能够想出组合allow-navigation and allow-intent这达到了我的目的。

<content src="https://example.com/" />
<access origin="*" />
<allow-navigation href="about:*" />
<allow-navigation href="https://example.com/*" />
<allow-navigation href="https://*.example.com/*" />
<allow-navigation href="https://*.facebook.com/*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />

Now 来自网站域的所有内部链接都会加载到应用程序中, while 外部链接加载到系统浏览器中.

注意,我包括了<allow-navigation href="https://*.facebook.com/*" />允许加载 Facebook 库,否则我会收到错误。

ERROR Internal navigation rejected - 
<allow-navigation> not set for url='https://staticxx.facebook.com/connect/xd_arbiter.php?

并且还包括<allow-navigation href="about:*" />以避免错误about:blank.

ERROR Internal navigation rejected - <allow-navigation> not set for url='about:blank'

希望这可以帮助其他遇到同样问题的人:)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Cordova Wrapper 应用程序,内部链接在应用程序中加载,外部链接在浏览器中加载 的相关文章

随机推荐