Iphone Cordova-Safari中始终打开内部超链接

Iphone Cordova-Safari中始终打开内部超链接,iphone,cordova,Iphone,Cordova,我对科尔多瓦很陌生,所以我可能不完全了解它的用途。让我从我试图实现的总体目标开始 我们有一个支持移动的asp.net网站,我基本上只是想用一个iPhone应用程序来包装它。该网站当然在IIS服务器上运行,因此我只需要一个薄包装器来启动该网站,并删除地址栏、导航等。据我所知,您可以通过Cordova混合方法实现这一点 我按照教程的要求,在xCode iPhone模拟器中启动了该网站,它就像我想要的那样出现了 我遇到的问题是,站点中的超链接会在Safari浏览器中启动目标页面。从我所有的谷歌搜索结果

我对科尔多瓦很陌生,所以我可能不完全了解它的用途。让我从我试图实现的总体目标开始

我们有一个支持移动的asp.net网站,我基本上只是想用一个iPhone应用程序来包装它。该网站当然在IIS服务器上运行,因此我只需要一个薄包装器来启动该网站,并删除地址栏、导航等。据我所知,您可以通过Cordova混合方法实现这一点

我按照教程的要求,在xCode iPhone模拟器中启动了该网站,它就像我想要的那样出现了

我遇到的问题是,站点中的超链接会在Safari浏览器中启动目标页面。从我所有的谷歌搜索结果来看,这似乎是大多数人面临的相反问题。似乎大多数人都在为在应用程序中打开外部站点而挣扎,这基本上将他们锁定在应用程序之外。我只是想在我自己的应用程序中从第1页转到第2页

我能够用最简单的站点重现这个问题,所以我将发布相关的信息。在本例中,单击“第2页”将在Safari中打开

Asp.net网站:

页面1.html

<html>
    <a href="page2.html">Page 2</a>
</html>

页面2.html

<html>
    Page 2
</html>

第2页
科尔多瓦:

Index.html

<!DOCTYPE html>
<html>
    <head>
        <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">
    </head>
    <body>
        Cordova site
    </body>
    <script>
        window.location = "http://192.168.1.157:8081/Page1.html";
    </script>
</html>

科尔多瓦遗址
window.location=”http://192.168.1.157:8081/Page1.html";
config.xml

<?xml version='1.0' encoding='utf-8'?>
<widget id="vsisolutions.testsite" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>Test Site</name>
    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>
    <author email="dev@cordova.apache.org" href="http://cordova.io">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <plugin name="cordova-plugin-whitelist" spec="1" />
    <access origin="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <allow-navigation href="http://192.168.1.157:8081/*" />
    <allow-navigation href="*" />
    <platform name="android">
        <allow-intent href="market:*" />
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
    </platform>
    <engine name="ios" spec="~4.1.1" />
    <plugin name="com.msopentech.authdialog" spec="~0.1.6" />
</widget>

试验场地
响应deviceready事件的Apache Cordova应用程序示例。
阿帕奇科尔多瓦团队
谢谢你的帮助


<a href="page2.html" target="_blank">Page 2</a>

这应该是可行的。

我发现在Cordova中,WKWebView插件(也可能出现在UIWebView中)会询问其他插件,看看它们是否可以使用链接上的URL。CDVIntentAndNavigationFilter将获取此信息,并通过逻辑运行,如所示:

- (BOOL)shouldOverrideLoadWithRequest:(NSURLRequest*)request navigationType:    (UIWebViewNavigationType)navigationType
{
   NSURL* url = [request URL];

 switch (navigationType) {
    case UIWebViewNavigationTypeLinkClicked:
        // Note that the rejection strings will *only* print if
        // it's a link click (and url is not whitelisted by <allow-*>)
        if ([self.allowIntentsWhitelist URLIsAllowed:url]) {
            // the url *is* in a <allow-intent> tag, push to the system
            [[UIApplication sharedApplication] openURL:url];
            return NO;
        }
        // fall through, to check whether you can load this in the webview
    default:
        // check whether we can internally navigate to this url
        return ([self.allowNavigationsWhitelist URLIsAllowed:url]);
    }
}
-(BOOL)应重写AdWithRequest:(NSURLRequest*)请求导航类型:(UIWebViewNavigationType)导航类型
{
NSURL*url=[请求url];
开关(导航类型){
案例UIWebViewNavigationTypeLinkClicked:
//请注意,拒绝字符串将*仅*在以下情况下打印:
//这是一个链接点击(并且url不会被列入白名单)
if([self.allowinentswhitelist url被允许:url]){
//url*在标记中,推送到系统
[[UIApplication sharedApplication]openURL:url];
返回否;
}
//下载,以检查是否可以在webview中加载此内容
违约:
//检查是否可以在内部导航到此url
返回([self.allowNavigationsWhitelist url被允许:url]);
}
}
因为navigationType==UIWebViewNavigationTypeLinkClicked,所以它通过[[UIApplication sharedApplication]openURL:url]将其传递给浏览器

目前,我只发现了一种破解方法,即通过以相同的方式处理链接来覆盖此逻辑,即:

switch (navigationType) {
  case UIWebViewNavigationTypeLinkClicked:
    // Note that the rejection strings will *only* print if
    // it's a link click (and url is not whitelisted by <allow-*>)
    if ([self.allowIntentsWhitelist URLIsAllowed:url]) {
        // the url *is* in a <allow-intent> tag, push to the system
       // [[UIApplication sharedApplication] openURL:url];
        return YES;
    }
    // fall through, to check whether you can load this in the webview
  default:
    // check whether we can internally navigate to this url
    return ([self.allowNavigationsWhitelist URLIsAllowed:url]);
    }
}
开关(导航类型){
案例UIWebViewNavigationTypeLinkClicked:
//请注意,拒绝字符串将*仅*在以下情况下打印:
//这是一个链接点击(并且url不会被列入白名单)
if([self.allowinentswhitelist url被允许:url]){
//url*在标记中,推送到系统
//[[UIApplication sharedApplication]openURL:url];
返回YES;
}
//下载,以检查是否可以在webview中加载此内容
违约:
//检查是否可以在内部导航到此url
返回([self.allowNavigationsWhitelist url被允许:url]);
}
}
这显然不理想,我会在Cordova论坛上四处询问,寻找更好的解决方案,一旦找到,我会在这里发布。

这是

它是固定的


因此,您不必再进行任何黑客操作,只需使用
允许导航
标记设置您希望允许在应用程序内部导航的URL,其余部分将在safari中打开,因为您为所有http和https URL设置了
allow intent

只需将
allow navigation
更改为append with
*
即可:

<allow-navigation href="http://yourwebsite/*" />


尝试添加target=“\u blank”,但没有成功。“_blank”会让它更容易在浏览器中打开,不是吗?也尝试过self,但似乎也没有什么不同。这是因为当webview决定如何处理url时,它会寻找任何可以帮助的插件。看起来有人在回应。我现在正在更详细地研究这个问题。我刚刚发现了这个可能的欺骗,它与我的解决方案相同:有一个关于这个问题的bug报告似乎已经在做了。感谢您跟踪。将已接受的答案更改为4.2.0版本,但由于这是当时唯一的解决方案,因此请继续投票。谢谢!直到我读到这篇文章,我才完全理解意图与导航,这篇文章澄清了很多问题,并使我的链接正常工作(至少到目前为止)。