Cordova Wrapper App内部链接在App中加载,外部链接在浏览器中加载

时间:2016-05-10 00:58:43

标签: javascript android ios cordova ionic-framework

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

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

在包含观看次数的普通应用中,我可以设置target='_system'以在默认浏览器中加载链接,或使用cordova-plugin-inappbrowser在网络浏览器视图中明确打开链接。不幸的是,在这种情况下,我无法编辑服务器端代码,因此需要一个适用于该应用程序的解决方案。

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

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

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

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

Otherssuggested使用自定义javascript覆盖target_system,但由于我没有自己的观点,所以我无法真正做到这一点。

是否可以为cordova-plugin-whitelist定义allow-intent,以便包含所有非内部域的网址?

或者我是否需要覆盖shouldStartLoadWithRequest中的MainViewController,然后拨打[[UIApplication sharedApplication] openURL:url]

1 个答案:

答案 0 :(得分:8)

好的,经过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'

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