Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript iOS UIWebView应用程序在Safari中打开链接_Javascript_Ios_Uiwebview - Fatal编程技术网

Javascript iOS UIWebView应用程序在Safari中打开链接

Javascript iOS UIWebView应用程序在Safari中打开链接,javascript,ios,uiwebview,Javascript,Ios,Uiwebview,我有一个iOS应用程序,它只有一个视图,那就是UIWebView(它打开应用程序的主要内容)。 我希望当我在某个地方(例如在表格行上)单击应用程序时,它会在Safari浏览器中打开特定的链接,而不是在UIWebView中 不编写任何iOS代码,而是让JavaScript在Safari中打开该链接,这样做可行吗 我有那个代码,但它一点帮助都没有: HTML代码 <tr class='link-to-pdf' data-href='www.example.com'> 在某些情况下,您可

我有一个iOS应用程序,它只有一个视图,那就是UIWebView(它打开应用程序的主要内容)。 我希望当我在某个地方(例如在表格行上)单击应用程序时,它会在Safari浏览器中打开特定的链接,而不是在UIWebView中

不编写任何iOS代码,而是让JavaScript在Safari中打开该链接,这样做可行吗

我有那个代码,但它一点帮助都没有:

HTML代码

<tr class='link-to-pdf' data-href='www.example.com'>

在某些情况下,您可以告诉javascript中具有
target=“\u blank”
的所有链接,并使用“\u system”参数将它们传递给window.open。这将在iOS和Android上都适用

$(document).on('click', 'a[target="_blank"]', function(ev) {
  var url;

  ev.preventDefault();
  url = $(this).attr('href');
  window.open(url, '_system');
});
或者,在您的情况下,只需替换
a.setAttribute(“target”,“_blank”)带有
a.setAttribute(“目标”,“_系统”)


这在一个古老的项目中对我有效,但我不确定它是否仍然有效(在iOS和Android上)

如果上面的答案对你无效,你可以用iOS的方式修复它。为此,您需要实现用于检查URL的
webView:shouldStartLoadWithRequest
UIWebViewDelegate
协议方法

 - (BOOL) webView: (UIWebView *) theWebView shouldStartLoadWithRequest:(NSURLRequest *) request navigationType: (UIWebViewNavigationType) navigationType
{
    NSURL *url = [request URL];
    // check URL in your if condition
    if (...) {
        //open URL in Safari
        [[UIApplication sharedApplication] openURL:url];
        return NO;
    }
    else
        return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
上述答案存在一个约束,您需要实现从JQuery通知ios应用程序的机制,这里讨论了可能的解决方法:

 - (BOOL) webView: (UIWebView *) theWebView shouldStartLoadWithRequest:(NSURLRequest *) request navigationType: (UIWebViewNavigationType) navigationType
{
    NSURL *url = [request URL];
    // check URL in your if condition
    if (...) {
        //open URL in Safari
        [[UIApplication sharedApplication] openURL:url];
        return NO;
    }
    else
        return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}