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



我有一个简单的Cordova包装应用程序,它指向外部网页,而不定义任何自己的视图。

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

在具有视图的普通应用程序中,我可以将target='_system'设置为在默认浏览器中加载链接,或者在应用程序浏览器中使用cordova插件显式打开web浏览器视图中的链接。不幸的是,在这种情况下,我无法编辑服务器端代码,因此需要一个在应用程序中工作的解决方案。

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

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

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

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

其他人建议使用自定义javascript来覆盖target_system,但是由于我没有自己的视图,所以我真的不能这么做。

是否可以为cordova插件白名单定义allow-intent,以包括所有非内部域的URL

或者我需要以某种方式覆盖MainViewController中的shouldStartLoadWithRequest,然后调用[[UIApplication sharedApplication] openURL:url]

好的,经过Hayyaan的一些实验和建议,我能够想出allow-navigationallow-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://*/*" />

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

注意,我包含了<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'

希望这能帮助其他有同样问题的人:)

最新更新