iOS UIWebView应用程序在Safari中打开链接 [英] iOS UIWebView app opens link in Safari

查看:117
本文介绍了iOS UIWebView应用程序在Safari中打开链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an iOS app which has only one view and that is UIWebView (It opens the main content of the app). I would like when I make a click somewhere(e.g on a table row) the app to opens specific link in Safari browser not inside the UIWebView.

在没有编写任何iOS代码的情况下是否可行,而不是那样使JavaScript在Safari中打开该链接?

Is that doable without writing any iOS code and instead of that make the JavaScript opens that link in Safari itself ?

我有该代码,但它根本没有帮助:

I have thatcode,but it doesn't help at all:

HTML代码

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

JS代码:

 $(".link-to-pdf").click(function () {

            var a = document.createElement('a');
            a.setAttribute("href", this.getAttribute("data-href"));
            a.setAttribute("target", "_blank");

            var dispatch = document.createEvent("HTMLEvents");
            dispatch.initEvent("click", true, true);
            a.dispatchEvent(dispatch);

        });


推荐答案

在某些情况下,您可以告诉您的所有链接具有 target =_ blank的javascript,并使用'_system'参数将它们传递给window.open。这适用于iOS和Android。

In some cases, you can tell all links in your javascript that have target="_blank", and pass them to window.open with the '_system' param. This will work on both iOS and 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); with a.setAttribute(target,_ system);

Or in your case, simply replace a.setAttribute("target", "_blank"); with a.setAttribute("target", "_system");

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

This worked for me in an ancient projekt, but im unsure if it still works (on iOS and Android)

这篇关于iOS UIWebView应用程序在Safari中打开链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆